RollingFileAppender

The RollingFileAppender extends the functionality of the FileAppender by adding the ability to automatically roll over log files based on specified criteria, such as file size or time intervals.

This appender is useful for managing log files in production environments where log data grows over time and older logs should be preserved while maintaining a manageable file size.

The following example shows how to configure the RollingFileAppender to write to the file MyApp.log.

The file written to will always be named MyApp.log because the StaticLogFileName parameter is specified.

The file will be rolled based on a size constraint (RollingStyle), and old log files will be archived when the size limit is reached. Up to 10 (MaxSizeRollBackups) old files of 100 KB each (MaximumFileSize) will be kept.

These rolled files will be named: MyApp.log.1, MyApp.log.2, MyApp.log.3, etc.

This allows you to keep a set number of backup log files, which can be helpful for both auditing and troubleshooting, while ensuring that log file growth doesn’t fill up disk space.

<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
  <file value="MyApp.log" />
  <appendToFile value="true" />
  <rollingStyle value="Size" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="100KB" />
  <staticLogFileName value="true" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
  </layout>
</appender>

This example show how to configure the RollingFileAppender to roll log files on a date period.

This example will roll the log file every minute. To change the rolling period adjust the DatePattern value. For example, a date pattern of "yyyyMMdd" will roll every day.

See System.Globalization.DateTimeFormatInfo for a list of available patterns.

<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
  <file value="MyApp.log" />
  <appendToFile value="true" />
  <rollingStyle value="Date" />
  <datePattern value="yyyyMMdd-HHmm" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
  </layout>
</appender>

This example show how to configure the RollingFileAppender to roll log files on a date period and within a date period on file size.

For each day only the last 10 files of 1MB will be kept.

<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
  <file value="MyApp.log" />
  <appendToFile value="true" />
  <rollingStyle value="Composite" />
  <datePattern value="yyyyMMdd" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="1MB" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
  </layout>
</appender>

This example show how to configure the RollingFileAppender to roll log files once per program execution.

The appendToFile property is set to false to prevent the appender from overwriting the existing files.

The maxSizeRollBackups is set to negative 1 to allow an infinite number of backup files.

The file size does have to be limited but here it is set to 50 Gigabytes which, if a log file exceeds this size limit during a single run then it will also be rolled.

<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
  <file value="MyApp.log" />
  <appendToFile value="false" />
  <rollingStyle value="Size" />
  <maxSizeRollBackups value="-1" />
  <maximumFileSize value="50GB" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
  </layout>
</appender>