1、NLog配置文件中配置项说明
2、NLog按小时归档配置
1)使用fileName="${basedir}/logs/${cached:${date:format=yyyy-MM-dd HH_mm_ss}}.log"
的方法,以确保每个应用程序实例一个日志文件。
2)用于archiveFileName="${archiveLogDirectory}/LogFile.log.{####}"
在末尾附加数字(#
根据需要随意添加或删除,具体取决于maxArchiveFiles
的配置)。
3)使用archiveNumbering="Sequence"
(数字越大日志越新)的顺序。还可以配置成 Rolling
、Date
、DateAndSequence
。
配置内容:
<?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="Error" internalLogFile="./internal-nlog.txt" throwExceptions="true"> <variable name="logDirectory" value="./logs"/> <variable name="archiveLogDirectory" value="./logs/archive"/> <targets> <target name="errors" xsi:type="File" fileName="${logDirectory}/${cached:${date:format=yyyy-MM-dd HH_mm_ss}}.log" archiveFileName="${archiveLogDirectory}/LogFile.log.{#}" maxArchiveFiles="9" archiveEvery="Hour" archiveNumbering="Sequence" /> </targets> <rules> <logger name="*" minlevel="Debug" writeTo="errors"/> </rules> </nlog>
如果使用Log4Net的,可以参数下面的配置:
<appender name="Work" type="RMM.Common.Logger.LogFileRollingAppender, Common"> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <dateTimeStrategy type="log4net.Appender.RollingFileAppender+UniversalDateTime" /> <file type="log4net.Util.PatternString" value="%property{EdgeAPILogPath}\WebAPI_Work.log" /> <param name="AppendToFile" value="true"/> <rollingStyle value="Once" /> <rollingStyle value="Composite" /> <datePattern value=".yyyyMMdd-HH" /> <maxSizeRollBackups value="300" /> <maximumFileSize value="20MB" /> <Encoding value="UTF-8" /> <staticLogFileName value="true" /> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%utcdate{yyyy/MM/dd HH:mm:ss,fff}	[%-5p]	[%3t]	%m%n" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <levelMin value="DEBUG" /> <levelMax value="FATAL" /> </filter> </appender>