Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4Net RollingFileAppender with composite rolling style is overwritting data

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.

like image 416
Tai Squared Avatar asked Apr 09 '09 16:04

Tai Squared


People also ask

What is RollingFileAppender in log4net?

RollingFileAppender can roll log files based on size or date or both depending on the setting of the RollingStyle property. When set to Size the log file will be rolled once its size exceeds the MaximumFileSize.

What is Maxsizerollbackups in log4net?

Gets or sets the maximum number of backup files that are kept before the oldest is erased.

What are log4net Appenders?

For log4net to know where to store your log messages, you add one or more appenders to your configuration. An appender is a C# class that can transform a log message, including its properties, and persist it somewhere. Examples of appenders are the console, a file, a database, an API call, elmah.io, etc.

Where are log4net logs stored?

The log4net. config file is located in the \Enterprise7\bin\ directory and is the configuration file for the log4net logging application.


2 Answers

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

like image 179
dommer Avatar answered Oct 12 '22 09:10

dommer


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>
like image 45
Eddie Avatar answered Oct 12 '22 10:10

Eddie