Log4Net RollingFileAppender with composite rolling style is overwritting data
Asked Answered
U

2

13

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.

Unidirectional answered 9/4, 2009 at 16:28 Comment(2)
Just making sure I understand: you want to roll on date and size? or just date?Carlcarla
I want it to roll on just the date. By default, though, the rolling is set to composite, so it rolls on date or size, whichever comes first.Unidirectional
J
15

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

Jotunheim answered 9/4, 2009 at 16:44 Comment(0)
F
11

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>
Fantasize answered 10/4, 2009 at 4:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.