- 在项目中使用 NuGet 安装
NLog
包以及NLog.Config
包
- 配置
nlog.config
在项目的根目录下创建一个 Nlog.config
文件(如果还没有),然后添加如下配置:
<?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">
<!-- 定义日志目标 async="true" 异步写日志-->
<targets async="true">
<!-- log_file 配置,按天生成日志文件夹,保留时间7天 -->
<target name="log_file" xsi:type="File"
fileName="logs/${shortdate}/LogFile_${shortdate}.log"
layout="${longdate}|${level:uppercase=true}|${message}${onexception:${newline}${exception:format=tostring}}${newline}"
archiveEvery="Day"
archiveNumbering="Rolling"
maxArchiveDays="7"
archiveAboveSize="5242880"
keepFileOpen="false"
concurrentWrites="true" />
<!-- err_file 配置 -->
<target name="err_file" xsi:type="File"
fileName="logs/${shortdate}/ErrorFile_${shortdate}.log"
layout="${longdate}|${level:uppercase=true}|${messa