Log4Net, RollingFile named by Date and the staticLogFileName setting

· December 13, 2011

We have been chasing some strange logging bugs for a while in my current project. We are using log4net to do our logging and it works fine … until a couple of weeks ago. Some logging didn’t occur, in another case we didn’t get new files…

The fix is, very simple, but quite surprising and I thought I’ll share something on what we did to fix it.

We are using a RollingFileAppender and common strategy for how to handle the log files; we’re creating a new file for each new date. In order to achieve this we have set the following configuration:

      <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
         <file value="{a path here}" />
         <appendToFile value="true" />
         <rollingStyle value="Date" />
         <datePattern value="yyyyMMdd" />
         <layout type="log4net.Layout.PatternLayout">
           <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
         <filter type="log4net.Filter.LevelRangeFilter">
           <levelMin value="INFO" />

OK – nothing strange here really. And actually it worked fine for a long time. But all of a sudden we have ran into the problems I described above.

The solution is simply to add the “staticLogFileName” element to the configuration above, setting the value to false;

<staticLogFileName value="false" />

And with that it started to work again. I found some questions around this on StackOverflow that was related but not exactly this.

So if you name your RollingFiles logs with log4net with Dates – be sure to include the staticLogFileName = false setting.

Twitter, Facebook