Search code examples
c#nlog

NLog generates too many archives


I'm using NLog configured this way:

var logfile = new NLog.Targets.FileTarget("logfile")
{
    FileName = Path.Combine(_logDirectory, "myApp.log"),
    Layout = "${date} [${level}] ${message} ${exception}${newline}",
    ArchiveFileName = Path.Combine(_logDirectory, "myApp.{#}.log"),
    ArchiveAboveSize = 100 * 1024, MaxArchiveFiles = 9
};

On the file target syntax documentation it says that the number of {#} characters in the ArchiveFileName property are used to determine the number of digits. As you can see I've only put one hash character but the result is that I'm getting an archiving that goes way beyond myApp.9.log, it can easily reach past .100. What am I doing wrong?

Thanks for your help


Solution

  • I'm guessing that you want rolling filenames. Try adding ArchiveNumbering = NLog.Targets.ArchiveNumberingMode.Rolling.

    See also https://github.com/NLog/NLog/wiki/FileTarget-Archive-Examples