C# – Rollover on Log4Net

clog4net

I read an article which told that :

rollingStyle can be either Date, Size or Composite. the default setting Composite, uses a combination of Size and Date settings. Thus if you have the datePattern set to “.yyyy-MM-dd” and maxSizeRollBackups set to 10, themn it will maintain 10 log backups for each day.
If you have the DatePattern set to “.yyyy-MM-dd HH:mm” and maxSizeRollbackups = 10 then it will maintain 10 logfile backups per minute

and it said :

staticLogFileName indicates whether you need to keep writing (log) to the same file all the time. You will need to set it to false when using Date as the rolling style and you have large number of backups.

so i do it in my App.config :

<appender name="FileAppender" type="log4net.Appender.FileAppender">

  <file value="E:\operativity.log" />
  <staticLogFileName value="False" />
  <appendToFile value="true" />
  <rollingStyle value="Date" />
  <datePattern value=".yyyy-MM-dd HH:mm" />
  <maxSizeRollBackups value="3" />
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date{dd/MM/yyyy HH:mm:ss.fff} - %level - %message%newline" />
  </layout>
</appender>

as you see, i set rollingStyle to Date and datePattern to .yyyy-MM-dd HH:mm and
set maxSizeRollbackups to 3, and i set staticLogFileName to False, so it should take three logs in every minute on separate files, but it does not!! what is the problem?

NOTE: this the Link of article

EDIT: I use it in a windowsservice and it gives me just one log file!

enter image description here

Best Answer

The problem is that you have a : (colon) in the datePattern, its an invalid char for filenames.

you also have to use RollingFileAppender not FileAppender.

Change

<datePattern value=".yyyy-MM-dd HH:mm" />

To

<datePattern value=".yyyy-MM-dd-HHmm" />

You will get one file because first file doesnt contain the datetime. And it will fail creating the other files because it contains the : (colon)

This one is tested and it works:

  <log4net>
    <appender name="Debug" type="log4net.Appender.RollingFileAppender">
      <file value="E:\operativity3.log" />
      <appendToFile value="true" />
      <maxSizeRollBackups value="3" />
      <datePattern value=".yyyy-MM-dd-HHmm" />
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="INFO" />
        <param name="LevelMax" value="FATAL" />
      </filter>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] [%level] %logger - %message%newline" />
      </layout>      
    </appender>    
    <root>
      <level value="ALL" />
      <appender-ref ref="Debug" />
    </root>
  </log4net> 

And here is the result:

enter image description here

Related Topic