nlog使用小记(日志文件分割备份循环)

发布时间 2023-12-27 11:13:26作者: microsoft_xin

nlog使用小记(日志文件分割备份循环)

fileName: 当前记录日志的文件名
archiveFileName:归档日志文件名
archiveAboveSize:文件达到多大进行归档
maxArchiveFiles:归档的日志文件保留数量
archiveNumbering:归档文件名称的保留方式 Sequence- 按顺序保留,如:2023-12-27_0005.log,2023-12-27_0006.log,... Rolling- 滚动生成,文件名不变,如:2023-12-27_0001.log,2023-12-27_0002.log,...

以下是配置示例:

 

<?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="Warn" throwConfigExceptions="true">
	<!--autoReload 自动重新加载-->
	<!--throwExceptions 抛出异常-->
	<!--throwConfigExceptions="true" NLog 在配置文件错误的情况下抛出异常-->
	<!--在根节点(nlog)配置 internalLogLevel, internalLogFile,可以查看NLog输出日志时的内部信息,比如你配置文件有错误,很有帮助,不过项目发布后还是关闭比较好,以免影响效率;-->

	<extensions>
		<add assembly="NLog.Web.AspNetCore" />
	</extensions>
	<targets async="true">
		<!--黑洞 忽略的日志-->
		<target xsi:type="Null" name="blackhole" />
		<default-wrapper xsi:type="BufferingWrapper"  bufferSize="100" FlushTimeout="10000">
			<wrapper-target xsi:type="AsyncWrapper" queueLimit="5000" overflowAction="Discard"/>
		</default-wrapper>
		<target xsi:type="File" name="allfile" fileName="/tmp/bpm.log" 
				archiveFileName="/tmp/historys/${shortdate}_{####}.log" 
				archiveNumbering="Sequence" 
				maxArchiveFiles="5" 
				archiveAboveSize="10240" layout="${longdate} | ${threadid} | ${logger} | ${uppercase:${level}} | ${message} ${exception:format=tostring}" />
		<!--屏幕打印消息-->
		<target name="console" xsi:type="ColoredConsole" layout="${longdate}| ${logger} | ${uppercase:${level}} | ${message}"/>
	</targets>
	<rules>

		<!-- 记录应用程序的 database 输出 -->
		<!--<logger name="SmartCore.*" minlevel="database" writeTo="allfile" />-->
		<!-- 记录所有日志级别不低于 Warn 的日志到日志文件 -->
		<logger name="*" minlevel="Info" writeTo="allfile" />
		<logger name="*" minlevel="Info" writeTo="console" />
	</rules>
</nlog>