NLog - 结合保留天数和大小限制

NLog - combining retention days and size limit

ASP.NET 核心 使用 NLog,我想实现以下目标:


每天的日志文件。 文件最大 10MB。 最多保留 30 天的日志,自动删除旧日志。


"MaxArchiveFiles" 不符合此功能要求

如何配置?

我的当前设置。

<?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>

这是你需要的吗?一个说明:10Mb太小了,我指定100Mb,如果你想在archiveAboveSize字段中更改它。 例如 100MB = 104857600 字节

<?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>