Simon Elms
Simon Elms

Reputation: 19668

NLog: Retaining log files with dates in names for x days

I'd like to have a simple log file scheme with NLog: Create the log file with the current date in the name then retain, say, 31 days' worth of log files.

So something like:

2020-11-23.log  <-- Today's date
2020-11-22.log
...
2020-10-24.log
2020-10-23.log  <-- Remove this one as being over 31 days old

Is this possible?

The impression I get from reading the docs and various blogs is that there may be a problem using the same naming scheme for the live log file and the archived log files. Also, that I may need to put the archived log files in another folder (although all the blogs mentioning that were over four years old, so I don't know if that's still the case). However, the docs don't seem very detailed and I'm not sure how up to date the blog posts are that I've read.

Upvotes: 1

Views: 998

Answers (1)

Rolf Kristensen
Rolf Kristensen

Reputation: 19867

You can do this in NLog 4.5:

<target name="file" xsi:type="File"
        fileName="${basedir}/logs/App.${shortdate}.txt" 
        maxArchiveFiles="31" />

And it should just work, unless you start using archiveFileName=.

NLog 4.7 introduced the setting maxArchiveDays= to help when also using archiveAboveSize=.

If you want to use static filename together with archiveFileName=, then there are some examples here: https://github.com/NLog/NLog/wiki/FileTarget-Archive-Examples

Upvotes: 2

Related Questions