Click here to Skip to main content
11,494,174 members (62,228 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
Hi

I am trying to configure the log4net rolling file appender, so it would create a new log every minute. Below is my configuration.


<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

<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 11-May-13 14:55pm
koleraba1.2K
Edited 11-May-13 15:32pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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

<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>
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 448
1 OriginalGriff 275
2 Nakul Chaudhari 240
3 CHill60 185
4 CPallini 175
0 Sergey Alexandrovich Kryukov 10,277
1 OriginalGriff 8,805
2 Sascha Lefèvre 3,704
3 Maciej Los 3,392
4 Richard Deeming 2,500


Advertise | Privacy | Mobile
Web04 | 2.8.150520.1 | Last Updated 30 Mar 2015
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100