Labels

.NET (2) Civ (1) ClickOnce (1) Fun (1) LEGO (1) Log4Net (1) PdfManual (1) Video (1)

Friday, July 27, 2012

Rolling files on date/time boundaries doesn't support a maximum number of backup files.

A few years ago I posted a patch to Log4Net: https://issues.apache.org/jira/browse/LOG4NET-27

I don't remember much about it anymore, but I'll use this blog entry to maintain what I know.

This patch allowed rolling logs by date and deleting them after they are so old.
It was against version 1.2.10.0

My full source code can be downloaded at https://sites.google.com/site/mafujosh/Log4Net_ModifiedByJoshMasek.zip

An example of limiting files by date and by size:

     <appender name="WhateverYouNameThis" type="log4net.Appender.RollingFileAppender">
        <threshold value="All" />
        <file value="logs\WhateverYouNameThisFile.log" />
        <appendToFile value="true" />
        <maxDateRollBackups value="15" />
        <maxSizeRollBackups value="20" />
        <maximumFileSize value="5MB" />
        <rollingStyle value="Composite" />
        <staticLogFileName value="true" />
        <datePattern value="yyyyMMdd-HH.lo\g" />
        <layout type="log4net.Layout.PatternLayout">
            <param name="ConversionPattern" value="%d [%t] %-5p - %m%n" />
        </layout>
     </appender>

So, basically, every hour, this will create a new file (because the format changes every hour).  And, any files older than 15 days should get deleted.  Also, if a file gets larger than 5MB, it will roll to another file, up to 20 times, and then these will start getting deleted too if it goes over 20 files.

Also, some related stack overflow links:
http://stackoverflow.com/questions/95286/log4net-set-max-backup-files-on-rollingfileappender-with-rolling-date/
http://stackoverflow.com/questions/2290552/log4net-remove-old-files-rolling-by-date/
http://stackoverflow.com/questions/2063329/do-you-know-of-a-log4net-appender-which-rolls-on-date-but-lets-you-limit-the-t/

No comments: