Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NLog settings not deleting MaxArchiveDays or MaxArchiveFiles?

Tags:

c#

nlog

I have the following NLog config file below. It's set to Archive every day and initially I had just the MaxArchiveFiles. Now I would like to keep only X amount of days of archived files and found the info that says MaxArchiveDays is available in v4.7 and up. So, I upgraded to v4.7 but now it doesn't seem to archive either on Days or File number.

Anyone see anything wrong with this config file with NLog v4.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"
      autoReload="true" 
      throwExceptions="true">

  <variable name="LogDirectory" value="D:/Logs/HRImport"/>

  <targets async="true">

    <target name="DefaultTarget"
      xsi:type="File"
      fileName="${LogDirectory}/LogFile.log"
      encoding="utf-8"
      layout="${longdate} | ${callsite} | ${message}"
      archiveFileName="${LogDirectory}/Archive/${shortdate}_log.{#}.log"
      archiveAboveSize="3145728"   
      archiveEvery="Day"
      archiveNumbering="Rolling"
      maxArchiveFiles="3"
      maxArchiveDays="2"
    />
    <!--1 meg: 1048576 -->

    <target name="ConsoleTarget"
      xsi:type="Console"
      layout="${longdate} ${logger:shortName=True} ${message}${onexception:EXCEPTION OCCURRED\:${exception:format=type,message,StackTrace,method:maxInnerExceptionLevel=8:innerFormat=type,message,StackTrace,method}}"
    />

  </targets>

  <rules>
    <logger name="defaultLogger" minlevel="Debug" writeTo="DefaultTarget,ConsoleTarget" />
  </rules>
</nlog>

** UPDATE **

<target name="DefaultTarget"
      xsi:type="File"
      fileName="${LogDirectory}/LogFile.log"
      encoding="utf-8"
      layout="${longdate} | ${callsite} | ${message}"
      archiveFileName="${LogDirectory}/Archive/{#}_log.log"
      archiveNumbering="DateAndSequence"
      archiveAboveSize="3145728"   
      archiveEvery="Day"
      maxArchiveFiles="3"
      maxArchiveDays="2"
    />
like image 636
Caverman Avatar asked Mar 02 '23 13:03

Caverman


1 Answers

The problem is the use of ${shortdate}:

archiveFileName="${LogDirectory}/Archive/${shortdate}_log.{#}.log"
archiveNumbering="Rolling"

When using Layout in archiveFileName then it must be very static. Only {#} should contain the dynamic part.

Instead try this:

archiveFileName="${LogDirectory}/Archive/{#}_log.log"
archiveNumbering="DateAndSequence"

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

like image 85
Rolf Kristensen Avatar answered Mar 09 '23 08:03

Rolf Kristensen