Nlog: Can't keep one day of archived logs

Created on 3 Jul 2020  路  6Comments  路  Source: NLog/NLog

NLog version: (e.g. 4.4.13)
Platform: .NET Core 3

Current NLog config (xml or C#, if relevant)

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <targets>
        <target xsi:type="File" name="f" fileName="file.log" autoFlush="true" keepFileOpen="true" concurrentWrites="true" archiveFileName="file-{#}.log" archiveNumbering="Date" archiveDateFormat="yyyy-MM-ddThh-mm-ss" maxArchiveFiles="1" archiveEvery="Day">
            <layout xsi:type="JsonLayout">
                <attribute name="time" layout="${date:universalTime=false:format=yyyy-MM-ddTHH\:mm\:ss.fffZ}"/>
            </layout>
        </target>
    </targets>

    <rules>
        <logger name="*" minlevel="Info" writeTo="f" />
    </rules>
</nlog>

Current result: no archive files are kept
Expected result: one archive file is kept

Worked well in v4.0.0.0

This is very similar to #2775 which is closed as a dup of #1479, but the latter is still open and it is not clear whether its fix will actually address the issue.

Please note, this blocks our project from migration to .Net Core; will likely block others as well.

file-archiving file-target question

Most helpful comment

Works well, thank you! I think this issue can be closed.

All 6 comments

Hi! Thanks for opening your first issue here! Please make sure to follow the issue template - so we could help you better!

Attaching internal log that corresponds to the following manual test:

  • Set maxArchiveFiles="2"
  • Start the application, wait for some logging, stop. Log file is created, OK.
  • Change the date to +1 day
  • Start the application, wait for some logging, stop. One archive file is created, OK
  • Change the date to +1 day
  • Start the application, wait for some logging, stop. Second archive file is created, OK
  • Set maxArchiveFiles="1"
  • Change the date to +1 day
  • Start the application, wait for some logging, stop. All archive files are deleted, NOT OK

nlog-internal.txt

Have you tried dusing the new option MaxArchiveDays=1 on FileTarget introduced with NLog 4.7:

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <targets>
        <target xsi:type="File" name="f" fileName="file.log" autoFlush="true" keepFileOpen="true" concurrentWrites="true" archiveFileName="file-{#}.log" archiveNumbering="Date" archiveDateFormat="yyyy-MM-ddThh-mm-ss" maxArchiveDays="1" archiveEvery="Day">
            <layout xsi:type="JsonLayout">
                <attribute name="time" layout="${date:universalTime=false:format=yyyy-MM-ddTHH\:mm\:ss.fffZ}"/>
            </layout>
        </target>
    </targets>

    <rules>
        <logger name="*" minlevel="Info" writeTo="f" />
    </rules>
</nlog>

See also: https://github.com/NLog/NLog/wiki/File-target

Works well, thank you! I think this issue can be closed.

Guys, I spent about 1 hour to figure out why maxArchiveFiles="1" won't working.
It's not normal, that:
with maxArchiveFiles = 1, I will get the current log file only (i.e. NO archive files) :confused:
and with maxArchiveFiles = 2, I will get 2 archive files. - correct.

This is really a bug, and if you interpret it like a _feature_, there must be a big warning about it in the docs, and described workaround with maxArchiveDays...

Ups:
image

So, with archiveNumbering = "Rolling" workaround above will not work at all :(
Any advice, how to achieve this:

maxArchiveFiles = "1"
archiveFileName = "${basedir}/log/our/WDlog.{#}.txt"
archiveNumbering = "Rolling"
archiveEvery="Month"

1 current log and 1 archive log, updated every month.
Thanks.

cc @304NotModified @snakefoot

@kirsan31 You are very welcome to create a more logical implementation of maxArchiveFiles for NLog 5.0 (dev-branch) with a pull-request.

Maybe read the comments for #2775 and #1479 before doing the implementation.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

haythamabutair picture haythamabutair  路  3Comments

MaximRouiller picture MaximRouiller  路  3Comments

npandrei picture npandrei  路  3Comments

Jerefeny picture Jerefeny  路  3Comments

vasumsit picture vasumsit  路  3Comments