NLog doesn't delete old log files

Created on 18 Mar 2020  路  9Comments  路  Source: NLog/NLog

Issue (Question)

ASP.NET CORE Using NLog

I have some trouble , it doesn't delete old logs file.

"MaxArchiveFiles" and "maxArchiveDays" did not execute the action of deleting logs properly.

What I do wrong? Thanks!

<?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"
    internalLogLevel="info"
    internalLogFile="C:\Logs\testProject\nlog-internal.txt">

  <extensions>
    <add assembly="NLog.Web.AspNetCore"/>
  </extensions>


    <targets>
    <!-- write logs to file  -->
    <target xsi:type="File" name="ALL"
            fileName="C:\Logs\testProject\testProject.${cached:${date:format=yyyy-MM-dd}}.log"
            layout="${longdate}|${uppercase:${level}}|${threadid}|${logger}|${message} ${exception:format=ToString}" 
            maxArchiveFiles="20"
            archiveFileName="C:\Logs\testProject\testProject{#}.log"
            archiveDateFormat="yyyy-MM-dd"
            archiveAboveSize="104857600"
            archiveNumbering="DateAndSequence"
            maxArchiveDays="3"
            archiveEvery="Day"       
          />

    </targets>




    <rules>
        <logger name="*" minlevel="Trace" writeTo="ALL" /> 
    </rules>
</nlog>
duplicate file-archiving file-target question

Most helpful comment

Current files (files from today) are not part of archive when using MaxArchiveDays. So maxArchiveDays="1" will only clean files older than yesterday.

When you have specified a custom archiveFileName then it will be used as wildcard for cleanup (instead of dynamic wildcard). So you should change it so it matches the format of fileName (Added missing dot after testProject)

    <target xsi:type="File" name="ALL"
            fileName="C:\Logs\testProject\testProject.${cached:${date:format=yyyy-MM-dd}}.log"
            archiveFileName="C:\Logs\testProject\testProject.{#}.log"
            archiveDateFormat="yyyy-MM-dd"
          />

You could consider removing archiveFileName and archiveDateFormat and archiveNumbering, so it becomes this:

    <target xsi:type="File" name="ALL"
            fileName="C:\Logs\testProject\testProject.${cached:${date:format=yyyy-MM-dd}}.log"
            layout="${longdate}|${uppercase:${level}}|${threadid}|${logger}|${message} ${exception:format=ToString}"
            maxArchiveFiles="100"
            archiveAboveSize="10485760"
            maxArchiveDays="1"
          />

All 9 comments

Duplicate of #3807. See reply here

鈿狅笍 This has been marked to be closed in 7 days.

Thanks for the team's answer!!

Currently know that "archiveEvery" can be removed.
"maxArchiveFiles" is functional.

Is the date identified by "maxArchiveDays" the date of the archive name or the date the file was generated?

It just strips away all non-letters in filename so it this:

C:\Logs\testProject\testProject.2020-03-19.log

Becomes this file-wildcard used for cleanup (Using the timestamp on the file to see age):

C:\Logs\testProject\testProject*.log

Has your question been answered?

Still testing the implementation of "maxArchiveDays"

    <target xsi:type="File" name="ALL"
            fileName="C:\Logs\testProject\testProject.${cached:${date:format=yyyy-MM-dd}}.log"
            layout="${longdate}|${uppercase:${level}}|${threadid}|${logger}|${message} ${exception:format=ToString}" 
            maxArchiveFiles="100"
            archiveFileName="C:\Logs\testProject\testProject{#}.log"
            archiveDateFormat="yyyy-MM-dd"
            archiveAboveSize="10485760"
            archiveNumbering="DateAndSequence"
            maxArchiveDays="1"     
          />

Folder

testProject.2020-03-20
testProject.2020-03-19
testProject2020-03-19.11590
testProject2020-03-19.11591

maxArchiveDays="1"
Date of the 19th should be deleted

Current files (files from today) are not part of archive when using MaxArchiveDays. So maxArchiveDays="1" will only clean files older than yesterday.

When you have specified a custom archiveFileName then it will be used as wildcard for cleanup (instead of dynamic wildcard). So you should change it so it matches the format of fileName (Added missing dot after testProject)

    <target xsi:type="File" name="ALL"
            fileName="C:\Logs\testProject\testProject.${cached:${date:format=yyyy-MM-dd}}.log"
            archiveFileName="C:\Logs\testProject\testProject.{#}.log"
            archiveDateFormat="yyyy-MM-dd"
          />

You could consider removing archiveFileName and archiveDateFormat and archiveNumbering, so it becomes this:

    <target xsi:type="File" name="ALL"
            fileName="C:\Logs\testProject\testProject.${cached:${date:format=yyyy-MM-dd}}.log"
            layout="${longdate}|${uppercase:${level}}|${threadid}|${logger}|${message} ${exception:format=ToString}"
            maxArchiveFiles="100"
            archiveAboveSize="10485760"
            maxArchiveDays="1"
          />

Thanks team!

The problem is solved.

"MaxArchiveDays" is the date judgement time based on the date generated by the file.

Thank you very much for the prompt reply.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

haythamabutair picture haythamabutair  路  3Comments

smeegoan picture smeegoan  路  3Comments

vasumsit picture vasumsit  路  3Comments

ranjan-2209 picture ranjan-2209  路  3Comments

geedsen picture geedsen  路  3Comments