views:

3737

answers:

3

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.

+1  A: 

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).

dommer
+1  A: 

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>
Eddie
A: 

Please bear in mind if you define rollingStyle to Date but this does not support maxSizeRollBackups nor maximumFileSize. In other words, old files will always remain.

For example:

<file value="log.txt" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd"/>
<maxSizeRollBackups value="3" />
<maximumFileSize value="1000KB" />

Will influence in these files:

  • log.txt
  • log.txt20100520
  • log.txt20100519
  • log.txt20100518
  • log.txt20100517
  • ...

(You might have expected there would be only 3 files stored at a time)

You can define rollingStyle to Composite, but maxSizeRollBackups nor maximumFileSize will impact only the file of current date (depending on DatePattern setting).

Jaroslaw Dobrzanski