NLog maxArchiveDays delete old folders

Viewed 35
<target xsi:type="File" 
async="true" 
name="jsonFileTrace" 
keepFileOpen="true" 
OpenFileCacheTimeout="60"
archiveAboveSize="5242880"  
maxArchiveFiles="20"   
maxArchiveDays="10"  
archiveNumbering="DateAndSequence" 
AutoFlush="false" 
openFileFlushTimeout="5"  
ConcurrentWrites="false"
archiveFileName="${basedir}/logs/trace/archive/${shortdate}/trace.{#}.log"
fileName="${basedir}/logs/trace/${shortdate}.log">
  <layout xsi:type="JsonLayout" includeAllProperties="true">
    <attribute name="time" layout="${longdate}" />
  .......//other attributes
  </layout>
</target>

If I configed like above, it can be archived by date. But the folder 2022-09-20 cannot be deleted if current date is 2022-10-01 which already 10 days past since 2022-09-20

three folders archived by date

1 Answers

Use this configuration instead:

<target xsi:type="File" name="jsonFileTrace" 
   keepFileOpen="true" 
   archiveAboveSize="5242880" 
   autoFlush="false" 
   openFileFlushTimeout="5"  
   concurrentWrites="false"
   fileName="${basedir}/logs/trace/${shortdate}/trace.${shortdate}.log">
  <layout xsi:type="JsonLayout" includeAllProperties="true">
    <attribute name="time" layout="${longdate}" />
  .......//other attributes
  </layout>
</target>

And setup a scheduled-task on the machine (or in the application) that runs every day and removes sub-folders that are older than 10 days.

Related