Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4net rolling file appender file name format when maximumFileSize reached

We're using the log4net rolling file appender and have the following requirements for our log files:

  • A new log file at the start of each day, with the date in the filename
  • A maximum log file size of 500KB

The issue we are having is the file naming strategy when files hit 500KB: they get renamed with a .1 suffix. This is problematic as it breaks file association in Windows, so opening the files is (slightly) more of a chore.

The configuration we're using is:

<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
  <file value="c:\log\path" />
  <staticLogFileName value="false" />
  <appendToFile value="true" />
  <rollingStyle value="Composite" />
  <datePattern value=".yyyy-MM-dd.lo\g" />
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  <maxSizeRollBackups value="50" />
  <maximumFileSize value="500KB" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %-5level %message%newline" />
  </layout>
</appender>

Is there support for specifying the naming strategy used when our files hit the maximumFileSize?

like image 232
Richard Ev Avatar asked Jul 16 '12 11:07

Richard Ev


People also ask

What is rolling file Appender 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.

Does Log4net create directory?

When using a file appender, the destination folder does not have to exist. Log4net creates the folder. Using an administrator account, connect to the Coveo Master server.


1 Answers

Version 1.2.11 of log4net includes the PreserveLogFileNameExtension property on the RollingFileAppender. Setting the property to true will allow files to be rolled in the format logName.roll#.fileExt, keeping your file associations intact.

The entry inside the appender block would look like:
<param name="PreserveLogFileNameExtension" value="true" />

like image 67
Adam S Avatar answered Oct 18 '22 21:10

Adam S