johng
johng

Reputation: 181

Issue with no log4net Logger.Appenders even though log shows config successfully loaded

I would appreciate any help you can provide? Not sure what I am doing wrong...

I am creating a log4net compatibility library for my new Visual Studio tool ErrorUnit at https://github.com/JohnGoldInc/ErrorUnit.Logger_log4net

Line 37 of https://github.com/JohnGoldInc/ErrorUnit.Logger_log4net/blob/master/ErrorUnitLogger.cs has Count=0 Appenders even though my log shows that appenders were loaded:

16 namespace ErrorUnit.Logger_log4net
17 {
18    public class ErrorUnitLogger : ILogger
19    {
20        private static ILog log = LogManager.GetLogger(typeof(ErrorUnitLogger));
21
22        public IEnumerable<string> GetErrorUnitJson(DateTime afterdate)
23        {
24            var ErrorUnitJson = new ConcurrentBag<string>();
25            log4net.Util.LogLog.InternalDebugging = true; //todo remove
26            System.Diagnostics.Trace.Listeners.Add(new System.Diagnostics.TextWriterTraceListener(@"f:\Temp\Logger_log4net.log", "myListener"));
27            System.Diagnostics.Trace.AutoFlush = true;
28            System.Diagnostics.Trace.TraceInformation("Test Logger_log4net message.");
29
30            var config = log4net.Config.XmlConfigurator.Configure();
31            log = LogManager.GetLogger(typeof(ErrorUnitLogger));
32
33            // Parallel.ForEach(logs, log => {
34            var log4net_Logger = log.Logger as log4net.Repository.Hierarchy.Logger;
35            if (log4net_Logger != null)
36            {
37                Parallel.ForEach(log4net_Logger.Appenders.Cast<log4net.Appender.IAppender>(), appender =>

For the log ( the log without ellipsis is at https://github.com/JohnGoldInc/ErrorUnit.Logger_log4net/blob/master/README.md ):

devenv.exe Information: 0 : Test Logger_log4net message.
log4net: configuring repository [log4net-default-repository] using .config file section
log4net: Application config file is [F:\Documents\Visual Studio 2015\Projects\WebApplication1\WebApplication1.Tests\obj\Release]
log4net: Configuring Repository [log4net-default-repository]
log4net: Configuration update mode [Merge].
...
log4net: Created Appender [AdoNetAppender]
log4net: Adding appender named [AdoNetAppender] to logger [root].
log4net: Hierarchy Threshold []
...

Thanks!

Upvotes: 0

Views: 675

Answers (3)

Vivek Rathod
Vivek Rathod

Reputation: 68

I do not have enough reputation to add a comment to your answer... so adding a new answer here but this is more so a comment

From your log, it seems you are adding AdoNetAppender to the 'root' Logger, so if you wish to get all the appenders for 'root' you may need to access the 'root' logger (which sits at the top of the hierarchy). Accessing the parent Logger, in this case, worked for you because 'root' happens to be parent of ErrorUnitLogger you have...but that may not always be true in general

If you are not already aware, you can access 'root' logger like this

Hierarchy hierarchy = (Hierarchy)log4net.LogManager.GetRepository();

Logger rootLogger = hierarchy.Root;

Upvotes: 1

johng
johng

Reputation: 181

Was not looking at top level Logger, to get to top level logger refer to .Parent property:

var log4net_Logger = log.Logger as log4net.Repository.Hierarchy.Logger;

while (log4net_Logger != null && log4net_Logger.Appenders.Count == 0)
    log4net_Logger = log4net_Logger.Parent;

Upvotes: 1

Peter
Peter

Reputation: 27944

You are sending your test message before you configure log4net. First configure log4net and than send the test trace message:

System.Diagnostics.Trace.TraceInformation("Test Logger_log4net message.");

Upvotes: 0

Related Questions