参考官方配置:File target · NLog/NLog Wiki · GitHub
NLog版本:4.0.0.0
.Net Framework:4.0
(1)每分钟归档一次,最多保留3个文件
<?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"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="false"
internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
<variable name="myvar" value="myvalue"/>
<targets>
<target name="logconsole" xsi:type="Console" />
<!--保存至文件-->
<target name="logfile" xsi:type="File" encoding="utf-8"
fileName="${basedir}/AppLogs/${date:format=yyyy}/${date:format=MM}/${shortdate}.txt"
archiveFileName="${basedir}/AppLogs/Archive/${shortdate}.{####}.txt"
maxArchiveFiles="3"
archiveEvery="Minute"
layout="RecordTime:${longdate} ThreadID:${threadid} Level:${level}${newline}LogMessage:${message} ${onexception:${exception:format=tostring} ${newline} ${stacktrace} ${newline}" />
</targets>
<rules>
<!--minlevel 大于等于当前级别的都会被记录下来-->
<logger name="*" minlevel="Debug" writeTo="logfile" />
</rules>
</nlog>
注意:
在 NLog 4.5 之前,如下截图中存档文件名中如果没有占位符,存档时NLog会把Archive目录下的所有其他文件删除,只保留3条,如下截图二
图一
图二
(2)每天归档一次,最多保留30个文件
<?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"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="false"
internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
<variable name="myvar" value="myvalue"/>
<targets>
<target name="logconsole" xsi:type="Console" />
<!--保存至文件-->
<target name="logfile" xsi:type="File" encoding="utf-8"
fileName="${basedir}/AppLogs/logfile.txt"
archiveFileName="${basedir}/AppLogs/Archive/Log{######}.txt"
maxArchiveFiles="30"
archiveEvery="Day"
layout="RecordTime:${longdate} ThreadID:${threadid} Level:${level}${newline}LogMessage:${message} ${onexception:${exception:format=tostring} ${newline} ${stacktrace} ${newline}" />
</targets>
<rules>
<!--minlevel 大于等于当前级别的都会被记录下来-->
<logger name="*" minlevel="Debug" writeTo="logfile" />
</rules>
</nlog>