Search code examples
log4net

Log4net randomly stops logging.


I am currently building an ASP.Net-MVC Application using log4net for logging, but the logger seems to just stop at random. It will happily log for awhile, and then stop, and then will start again after a period of time. I am not even sure what it is that makes it resume logging. I'm not talking about just a few messages being lost- sometimes it dissappears for a long period of time, such as an hour or so.

Why would it stop and start like this? How should I properly configure this so that it will not randomly stop as it does?

Here is my configuration:

<log4net debug="true">
<appender name="RollingLogFileAppender"
        type="log4net.Appender.RollingFileAppender">

  <file value="..\Logs\\CurrentLog.txt" />
  <appendToFile value="true" />
  <datePattern value="yyyyMMdd" />

  <rollingStyle value="Date" />
  <filter type="log4net.Filter.LevelRangeFilter">
    <acceptOnMatch value="true" />

    <levelMin value="INFO" />
    <levelMax value="FATAL" />
  </filter>

  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern
    value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
  </layout>

</appender>

<root>
  <level value="INFO" />
  <appender-ref ref="RollingLogFileAppender" />
</root>


Solution

  • Log4Net will fail silently if something goes wrong and it is unable to write to its appenders. This is actually a good thing, since it means a bit of failed logging won't bring down an otherwise healthy system, but it can be annoying when something isn't logging as you expect.

    Your best bet is to turn on log4net's own internal logging to do some diagnostics and (hopefully) work out why it's failing.

    So in your app's config file add:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <appSettings>
            <add key="log4net.Internal.Debug" value="true"/>
        </appSettings>
    </configuration>
    

    which will turn on the internal logging, which gets sent to System.Diagnostics.Trace, so you can add:

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

    to capture this to a file.