tags:

views:

118

answers:

1

How - if possible - can I use NLog as a rollover file logger? as if:

I want to have at most 31 files for 31 days and when a new day started, if there is an old day log file ##.log, then it should be deleted but during that day all logs are appended and will be there at least for 27 days.

+2  A: 

I would suggest that you separate the problem into two different aspects:

  • Rolling to a new file name each day (bear in mind the time zone; UTC day perhaps?)
  • Deleting old log files

In my experience it would be worth keeping the date in the log file name, e.g.

debug-2010-06-08.log

That part should be easy with NLog, given the examples in the docs.

Now the second part can easily be done in a second thread or possibly even a completely different process - it just needs to see how many files are present and delete the oldest ones if necessary. Finding the "oldest" should be easy if the date is in the filename, even if you don't want to trust the file system information.

In fact, looking at the NLog documentation, it looks like the "time-based file archival" target may do exactly what you want... but even if it doesn't, the normal "one log file per day" approach and rolling your own clean-up should be easy.

Jon Skeet