Search code examples
loggingnlogarchive

NLog: Archive and delete file


I'm trying to configure NLog to archive and delete old log files. What I want to do is to store the files in the archive folder, and delete files older than 5 days.

I'm using this configuration:

<variable name="logDirectory" value="${basedir}\logs"/>

 <targets>
   <target name="logfile" xsi:type="File" fileName="${logDirectory}\MyApp_${shortdate}.log"
           layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring,StackTrace}${newline}"
           maxArchiveFiles="5"
           archiveFileName="${logDirectory}\archive\MyApp-bkp_${shortdate}-{#}.log"
           archiveNumbering="Date" 
           archiveEvery="Day"
           archiveOldFileOnStartup="true"
           deleteOldFileOnStartup="true"           
   />

   <target name="logconsole" xsi:type="Console" />
 </targets>

But this configuration has no effects on log files.

Whats is wrong in my configuration?

Thanks in advance


Solution

  • If you remove ${shortdate} then it will work:

       <target name="logfile" xsi:type="File" fileName="${logDirectory}\MyApp.log"
               layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring,StackTrace}${newline}"
               maxArchiveFiles="5"
               archiveFileName="${logDirectory}\archive\MyApp-bkp_{#}.log"
               archiveNumbering="Date" 
               archiveEvery="Day"
               archiveOldFileOnStartup="true" />
    

    See also: https://github.com/NLog/NLog/wiki/FileTarget-Archive-Examples#archive-file-in-new-folder