Click here to Skip to main content
15,897,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I am trying to configure the log4net rolling file appender, so it would create a new log every minute. Below is my configuration.


XML
<appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
      <file type="log4net.Util.PatternString" value="Logs/Log_%date{dd.MM.yyyy-HH.mm}.log" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <maxSizeRollBackups value="6" />
      <datePattern value="yyyyMMdd-HHmm" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date{HH:mm:ss,fff}, %message%newline" />
      </layout>
    </appender>


With this configuration I get a new log every minute which is OK. The only problem is the name of the logs. The file name of the first log is as expected: Log_12.05.2013-01.43.log .
The problem is with the consecutive log files.
The next one is not named Log_12.05.2013-01.44.log as expected but
Log_12.05.2013-01.43.log20130512-0143

It seems I found a partial solution to my problem. The problem was with the "value" attribute of the file element, and the missing "preserveLogFileNameExtension" element.
Below is the updated configuration

XML
<appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
      <file type="log4net.Util.PatternString" value="Logs/Log_.log" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <datePattern value="yyyyMMdd-HHmm" />
      <maxSizeRollBackups value="6" />
      <preserveLogFileNameExtension value="true"/>
      <staticLogFileName value="false" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date{HH:mm:ss,fff}, %message%newline" />
      </layout>
    </appender>


But with the new configuration there is another problem. It seems that the "maxSizeRollBackups" element has no effect - the log4net is not deleting the old logs.

Uroš
Posted
Updated 11-May-13 14:32pm
v2

You need to replace value of rollingStyle By Composite and to add maximumFileSize element and no need maxSizeRollBackup element.You may delete maxSizeRollBackup element.

the updated configuration is below

XML
<appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
   <file type="log4net.Util.PatternString" value="Logs/Log_.log" />
   <appendToFile value="true" />
   <rollingStyle value="Composite" />
   <datePattern value="yyyyMMdd" />
   <maximumFileSize value="1KB" />
   <preserveLogFileNameExtension value="true"/>
   <staticLogFileName value="false" />
   <layout type="log4net.Layout.PatternLayout">
     <conversionPattern value="%date{HH:mm:ss,fff}, %message%newline" />
   </layout>
 </appender>
 
Share this answer
 
It seems that maxSizeRollBackups is not supported when rolling by date. For more information or if anybody needs that functionality follow this http://stackoverflow.com/questions/2290552/log4net-remove-old-files-rolling-by-date[^] link
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900