views:

356

answers:

3

Hi,

I need to define an appender for log4net in a way that I get one log file for each day, but the total number of files are limited to, let's say, 30. That is I want to keep only the logs not older then 30 days, delete the older ones.

I've tried doing it with RollingFileAppender, but it seems that specifying a limit of files to keep is not supported. Do you know of an alternative solution that I could use?

+1  A: 

Write a C# WinService or make in your application a BackgroundWorker that will monitor once a day your old log files and delete it. You can use the FileSystemWatcher for this scope.

Or just test at each application running the presence of new log files and delete it.

An other option will be do not store files locally but send it via network (mail, distant server etc).

See also duplicate questions: here and here.

serhio
+1  A: 

can you use the 'Composite' log type? more here: http://logging.apache.org/log4net/release/config-examples.html

 <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="logfile" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<datePattern value="yyyyMMdd" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="1MB" />
<layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>

bluevoodoo1
Correct me if I'm wrong, but I think this will actually create a new file each day, then, if log exceeds 1MB it will create another log file with the same date and when the number of logs for this date exceeds 10 it will delete them. It will not limit the total number of logs, though (per date).
Michał Drozdowicz
+2  A: 

I spent some time looking into this a few months ago. v1.2.10 doesn't support deleting older log files based on rolling by date. It is on the task list for the next release. I took the source code and added the functionality myself, and posted it for others if they are interested. The issue and the patch can be found at https://issues.apache.org/jira/browse/LOG4NET-27 .

Mafu Josh
Thanks! I'll have a look at your patch. I've eventually also modified the appender myself (well, subclassed it to be exact), but I only needed to deal with rolling by date (not by date and size), so I didn't consider actually making it public.
Michał Drozdowicz