Log4net randomly stops logging.

Viewed 18437

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>

4 Answers

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.

Also check if log4net is still configured in the AssemblyInfo.cs file. In my case a check-in caused the following line to be removed in this file:

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

Which in turn caused log4net to stop logging...

After struggling with Log4Net for a while, I found out that it would stop logging altogether. Not matter what I tried I couldn't get it back to work. It seems that there are issues when you do not initialize the log in the first few lines of code but that fuzzy indication was not enough for me to reliably get the log to work within a dll loaded by another program and so on.

I never managed to activate the internal log either.

I finally resolved to create a dedicated dll that my programs can call to reliably create a log using Log4Net. The dll does only that and the code creating the log is isolated and never changes. I assume that this way I will be able to reliably create logs files and write to them. So far this approach has fixed the issues with logging on which I had spend countless frustrating hours.

I posted the code on this SO thread:

stackoverflow.com/questions/308436/log4net-programmatically-specify-multiple-loggers-with-multiple-file-appenders

Related