Log4Net RollingFileAppender with composite rolling style is overwritting data

35,519

Solution 1

Here's my settings. It rolls only on date:

<log4net>
    <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
        <file value="c:\Logs\Today.log"/>
        <rollingStyle value="Date"/>
        <datePattern value="yyyyMMdd"/>
        <appendToFile value="true"/>
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%level %logger %date{ISO8601} - %message%newline"/>
        </layout>
    </appender>
    <root>
        <!-- Options are "ALL", "DEBUG", "INFO", "WARN", "ERROR", "FATAL" and "OFF". -->
        <level value="ERROR"/>
        <appender-ref ref="RollingFile"/>
    </root>
</log4net>

Changes to your web.config, will restart the application automatically (so you'll lose sessions, etc).

Solution 2

Try adding the maxSizeRollBackups parameter in your RollingFileAppender to solve half of our problem. This way, when the log file rolls, it won't overwrite your old log, but will roll it to another file.

<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender" >
  <param name="File" value="C:\\MyLog.log" />
  <param name="AppendToFile" value="true" />
  <param name="DatePattern" value="yyyy-MM-dd"/>
  <param name="maxSizeRollBackups" value="10" />
  <layout type="log4net.Layout.PatternLayout">
    <param name="ConversionPattern" value="%m%n"/>
  </layout>
</appender>
Share:
35,519
Tai Squared
Author by

Tai Squared

Java/C# software developer

Updated on October 16, 2020

Comments

  • Tai Squared
    Tai Squared over 3 years

    I have a Log4Net RollingFileAppender that is configured as:

    <configuration>
    
      <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
      </configSections>
    
      <log4net>
    
        <root>
          <level value="ALL" />
        </root>
    
        <logger name="RollingFileAppender" additivity="false">
          <level value="DEBUG"/>
          <appender-ref ref="RollingFileAppender" />
        </logger>
    
        <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender" >
          <param name="File" value="C:\\MyLog.log" />
          <param name="AppendToFile" value="true" />
          <param name="DatePattern" value="yyyy-MM-dd"/>
          <layout type="log4net.Layout.PatternLayout">
            <param name="ConversionPattern" value="%m%n"/>
          </layout>
        </appender>
    
      </log4net>
    
    </configuration>
    

    Looking at the documentation, the The default rolling style is Composite, so it makes sense this will roll when it reaches a certain size (the default of 10MB), not just on the date.

    The problem is when it hits the size, it is restarting the log and I am losing the data from the first half of the day (it reaches this size around noon).
    Why wouldn't this just roll to a new file and all future log lines are put into the MyLog.log? Or is it the log is rolling, but then at midnight, it is rolling again and overwritting the dated log (eg. rolling to MyLog.log2009-04-08 once it reaches 10MB, and then overwritting this same file at midnight)?

    I will set the

    <rollingStyle value="Date" />
    

    Is this all I have to do to ensure it only rolls on the Date boundary? Can I change this on the fly in the Log4Net.config, or do I have to restart the application? It is running on IIS6.