Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one troubleshoot log4net when it stops logging

Tags:

log4net

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 a specific log message and that seems to shut down the whole stack.

Is there a way to get Log4Net to throw an exeception (at least during our debug phase) rather than a slient shutting down of the service.

like image 648
Ralph Shillington Avatar asked Mar 24 '09 14:03

Ralph Shillington


People also ask

How to turn OFF log4net logging?

How do I completely disable all logging at runtime? Setting the Threshold on the Hierarchy to Level OFF will disable all logging from that Hierarchy. This can be done in the log4net configuration file by setting the "threshold" attribute on the log4net configuration element to "OFF".

What does log4net do?

log4net is a tool to help the programmer output log statements to a variety of output targets. In case of problems with an application, it is helpful to enable logging so that the problem can be located. With log4net it is possible to enable logging at runtime without modifying the application binary.


2 Answers

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.

like image 197
Eric Pohl Avatar answered Oct 02 '22 19:10

Eric Pohl


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.

like image 31
Andy White Avatar answered Oct 02 '22 20:10

Andy White