How does one troubleshoot log4net when it stops logging

后端 未结 3 828
悲&欢浪女
悲&欢浪女 2020-12-24 08:11

It seems that Log4Net silently shuts down for reasons that are not obvious, and I\'m at a loss how to troubleshoot it. My hunch is that a particular appender is failing on

相关标签:
3条回答
  • 2020-12-24 08:41

    Expanding on the previous answer -

    To add a trace listener for the log4net.Internal.Debug trace, add this to your app config:

      <system.diagnostics>
        <trace autoflush="true">
          <listeners>
            <add
                name="textWriterTraceListener"
                type="System.Diagnostics.TextWriterTraceListener"
                initializeData="c:\temp\log4net.txt" />
          </listeners>
        </trace>
      </system.diagnostics>
    

    Replace the initializeData attribute value above with your desired log file path. Be sure the application or ASP.NET server process has permission to write to this file.

    Another thing you can do is inspect the messages that are returned from the log4net configuration on startup. As of log4net version 1.2.11, the XmlConfigurator.Configure() methods return an ICollection containing strings listing problems encountered during the configuration process.

    So if you've got something like this:

    XmlConfigurator.Configure();
    

    change it to

    ICollection configMessages = XmlConfigurator.Configure();
    

    and inspect configMessages in a debugger, or print them out somewhere, e.g.

    foreach (string msg in configMessages)
    {
       Console.WriteLine(msg);
    }
    

    If all else fails, download the log4net source, add the project to your solution, and reference the project instead of log4net.dll. Now you can step into the log4net calls in the debugger.

    0 讨论(0)
  • 2020-12-24 08:45

    I think there's a config value you can put in the appSettings section of your app.config/web.config to turn on internal debug statements in log4net:

    <appSettings>
        <add key="log4net.Internal.Debug" value="true"/>
    </appSettings>
    

    This will give you some insight into any errors that log4net might be swallowing.

    0 讨论(0)
  • 2020-12-24 09:01

    I was having a tough time figuring out why the file log and sql log appenders were not working. I had changed the default SQL table and it turns out that the data type Int was no good and Int32 was the correct value. I couldn't seem to find this information until I was debugging in Visual Studio and stepped into the logging method. Execution of the method was paused on my breakpoint and I ran the setup command in the Immediate Window:

    log4net.Config.XmlConfigurator.Configure()

    I was able to see immediate feedback and there was in-fact an exception thrown when that configuration code ran. It displayed in the Immediate Window and I was able to address the issue.

    Hopefully this helps somebody.

    0 讨论(0)
提交回复
热议问题