RollingFileTraceListener max files

Aug 11, 2011 at 11:14 AM

Hi,

Is there a way to tell the RollingFileTraceListener that it should keep only a particular amount of the latest log files?

For example, I would like to keep only the last 10 files.

How can it be done?

Cheers.

Coordinator
Aug 12, 2011 at 1:16 PM

This is not directly supported by the listener.

The RollingFileTraceListener works similar to FileLogTraceListener and can roll to different files (e.g. on a daily basis) but doesn't delete old files.

You could use a separate process to clean up the log directory, e.g. if creating daily logs then use robocopy to move all files older than 10 days and then delete them:

 robocopy C:\Logs C:\Temp\ToDelete /move /minage:10

 rmdir /s C:\Temp\ToDelete

If you are interested in this as a feature, then add a Feature request to the Issue Tracker and then vote for it, but note that sometimes it could be a bit tricky, e.g. if you stop and restart the process how do you determine if there is a 10th file that needs to be deleted (pattern matching all the possible variations of the replacement tokens could be tricky).

It may need a different type of pattern to work reliably, e.g. a common pattern is use numbers "Foo.log", "Foo.log.1", "Foo.log.2", etc. Then when Foo.log is rolled over Foo.log.2 is deleted and all the others renamed and bumped up one step. (The difficultly here would be what basis the rolling is done on, e.g. keeping track of the date).

Another option could be to have fixed schedules like FileLogTraceListener, i.e. logFileCreationSchedule="None|Daily|Weekly", which gives a defined way to determine the last N files (compared to an arbitrary pattern e.g. "Foo_{DateTime:dd_HH}"),

Sly

 

Coordinator
Aug 13, 2011 at 11:04 AM

I added a feature in the issue tracker for this, so if it is important to you please vote.

 

Aug 15, 2011 at 8:47 AM

Thanks.

This is a very interesting feature, because allow you to let the log ON in a production application, and be sure that the log is never going to full the disk space, but ensuring you keep the latest activity. FileLogTraceListener just throws exceptions or skip the logging when it reaches the maximun space available, which is ridiculous! Could you imagine that a customer call me because the app is throwing exceptions because it is unable to log anymore? or that something is wrong in the app, and comes up that the application is not logging anything because the log space is full? that makes log useless.

I will take a look on how log4net does it next week, and maybe I can propose some ideas, or even help if you allow me to.

Regards.