NLog - combining retention days and size limit

Viewed 801

ASP.NET CORE Using NLog, I want to achieve the following:


log file per day. File maximum 10MB. Keep max 30 days of logs, automatically deleting older logs.


"MaxArchiveFiles" does not meet this feature requirement

How to configure this?

My current settings.

<?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">
    <targets>
    <!-- write logs to file  -->
        <target xsi:type="File" name="ALL"
            fileName="C:\Logs\TestProject\TestProject.log"
            layout="${longdate}|${uppercase:${level}}|${threadid}|${logger}|${message} ${exception}" 
            maxArchiveFiles="100"
            archiveFileName="C:\Logs\TestProject\TestProject.{###}.log"
            archiveAboveSize="10485760"
            archiveNumbering="Sequence"
            concurrentWrites="true"
            keepFileOpen="false"
          />
    </targets>
    <rules>
        <logger name="*" minlevel="Trace" writeTo="ALL" />
    </rules>
</nlog>
1 Answers

Is that what you need? One clarification: 10Mb is too small, I specify 100Mb, please change it if you want in archiveAboveSize field. e.g. 100MB = 104857600 bytes

<?xml version="1.0" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

     <targets>
        <target name="file" xsi:type="File"
            layout="${longdate} ${logger} ${message}${exception:format=ToString}" 
            fileName="${basedir}/logs/${cached:${date:format=yyyy-MM-dd HH_mm_ss}}.log"
            archiveFileName="${basedir}/logs/archive/{#}.log"
            archiveDateFormat="yyyy-MM-dd HH_mm_ss"
            archiveNumbering="DateAndSequence" 
            archiveAboveSize="104857600"
            archiveEvery="Day"
            maxArchiveDays="30" /> 
    </targets>

    <rules>
        <logger name="*" minlevel="Debug" writeTo="file" />
    </rules>
</nlog>
Related