Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NLog incorrect date when archiving

I want to use archiving to limit the number of log files, and I want the file name of each archived log to be the date which the log is from. Simple enough.

This is one of my targets:

<target xsi:type="File" name="info" fileName="${basedir}/logs/info.log"
        layout="${date:format=HH\:mm\:ss}&#009;|&#009;${uppercase:${level}}&#009;|&#009;${message}"
        archiveEvery="Day"
        archiveFileName="${basedir}/logs/archive/info/${shortdate}.{#}.log"
        archiveNumbering="Rolling"
        maxArchiveFiles="30"/>

I have read that you must have the {#} in the archiveFileName or the archiving won't work at all when you have the date in the file name, which is kind of annoying but I guess I can live with that.

However, because the archiving is performed after the date is changed, the ${shortdate} will always become the new day's date, i.e. one day (or more) after the desired date. If a message is logged today, 2013-06-12, then when it's archived tomorrow it will be placed in a file called 2013-06-13.log.

Is there any way to get the correct date? I have seen someone suggesting a variable, but I don't see how that would work... This just seems like such an obvious feature to have. It should definitely be one of the archiveNumbering modes! The numbering modes available now just seem so impractical compared to dates.

This question is kind of related to Delete log files after x days. If it was possible to set max number of log files (because that's what I actually want from the archiving feature) without using the archiving feature, that would actually be even better because I wouldn't have to use {#} either, but I suspect that isn't possible.

like image 306
moggizx Avatar asked Sep 03 '26 11:09

moggizx


1 Answers

NLog has since posting this question added the numbering format "Date" which is exactly what I was looking for.

See https://github.com/nlog/NLog/wiki/File-target#archival-options for more information.

like image 123
moggizx Avatar answered Sep 07 '26 14:09

moggizx