Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NLog - delete logs older than X days

How I can delete files with logs older than X days. It's simple, but I have in one folder logs only from one day. My NLog.config looks like:

<?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">
  <extensions>
    <add assembly="NLog.Extended" />
  </extensions>
  <variable name="LogHome" value="PATH"/>
  <variable name="DailyDir" value="${LogHome}${date:format=yyyy}/${date:format=MM}/${date:format=dd}/"/>
  <targets>
    <target name="asyncFile" xsi:type="AsyncWrapper">
      <target
        name="fatalLog"
        xsi:type="File"
        layout="${longdate}|${callsite}|${message}|${exception}"
        fileName="${DailyDir}/Fatal.txt"
              />
    </target>
    <target name="asyncFile" xsi:type="AsyncWrapper">
      <target
        name="errorLog"
        xsi:type="File"
        layout="${longdate}|${callsite}|${message}|${exception}"
        fileName="${DailyDir}/Error.txt"
              />
    </target>
  </targets>
  <rules>
    <logger name="*" level="Fatal" writeTo="fatalLog" />
    <logger name="*" level="Error" writeTo="errorLog" />
  </rules>
</nlog>
like image 870
piotrbalut Avatar asked Aug 18 '15 09:08

piotrbalut


3 Answers

right now you are creating logs in directories containing the date. To enable NLog to automatically manage your current and old log files, you need to use the NLog archiving functionality. As documented in the NLog file target documentation here you can use the attributes archiveFileName and maxArchiveFiles together with a daily log to keep log files for X days before NLog removes them.

You probably have to keep all archived logs in a single directory, otherwise NLog won't be able to locate the older logs and delete them. I would create an archive directory as a subdirectory of your main logging directory, have NLog put all archive logs there and then just use the maxArchiveFiles parameter to tell NLog how many of those logs you want to keep.

<targets>
<target name="asyncFile" xsi:type="AsyncWrapper">
  <target
    name="fatalLog"
    xsi:type="File"
    layout="${longdate}|${callsite}|${message}|${exception}"
    fileName="${LogHome}/Fatal.txt"
    archiveFileName="${LogHome}/Archive/Fatal-${shortdate}.txt"
    maxArchiveFiles="5"
    archiveEvery="Day"
          />
</target>
<target name="asyncFile" xsi:type="AsyncWrapper">
  <target
    name="errorLog"
    xsi:type="File"
    layout="${longdate}|${callsite}|${message}|${exception}"
    fileName="${LogHome}/Error.txt"
    archiveFileName="${LogHome}/Archive/Error-${shortdate}.txt"
    maxArchiveFiles="5"
    archiveEvery="Day"
          />
</target>
</targets>

that should give you two log files with the current log and an archive directory with 5 logs for each target from the last 5 days.

like image 147
Dirk Trilsbeek Avatar answered Oct 03 '22 00:10

Dirk Trilsbeek


NLog 4.5 makes it easier to setup archive cleanup:

  <target
    name="errorLog"
    xsi:type="File"
    layout="${longdate}|${callsite}|${message}|${exception}"
    fileName="${DailyDir}/Error.${shortdate}.txt"
    maxArchiveFiles="5"
  />

NLog 4.7 also introduces the setting maxArchiveDays (Useful if also using archiveAboveSize). See also: https://github.com/NLog/NLog/wiki/File-target#archive-old-log-files

like image 36
Rolf Kristensen Avatar answered Oct 03 '22 02:10

Rolf Kristensen


<target name="Logs" xsi:type="File" fileName="${basedir}/Logs/${shortdate}/${shortdate}-${level}.csv" archiveAboveSize="10240" keepFileOpen="false" 
        maxArchiveDays="30" maxArchiveFiles="90">
  <layout xsi:type="CSVLayout">
    <column name="time" layout="${longdate}" />
    <column name="logger" layout="${logger}"/>
    <column name="message" layout="${message}" />
  </layout>
</target>

I used the above code in my targets to overcome this problem. It will create a new file if the file increase 10MB and storing as CSV so it's easy to read in excel and deleting files 30 days old.

like image 5
hardik patel Avatar answered Oct 03 '22 01:10

hardik patel