首先添加negut包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:\nlog\nlog-internal.log"> <variable name="varDataFolderPath" value="${basedir}/logs"/>
<variable name="varLayHeader" value="${longdate}|${level}"/>
<variable name="varLayMiddle" value="${callsite-linenumber}|${callsite:includeSourcePath=false:methodName=false}"/>
<variable name="varLayBack" value="${message}|${exception:format=toString}"/>
<targets>
<target name="logFile"
xsi:type="File"
encoding="utf-8"
maxArchiveFiles=""
archiveEvery="Day"
archiveNumbering="Date"
archiveDateFormat="yyyyMMdd"
archiveFileName="${varDataFolderPath}/archives/log.{#}.txt"
fileName="${varDataFolderPath}/nlog.txt"
layout="${varLayHeader}|${varLayMiddle}|${varLayBack}" />
<target xsi:type="Console"
name="logConsole"
layout="${varLayHeader}|${varLayMiddle}|${varLayBack}" />
</targets>
<rules>
<logger minlevel="Trace" name="*" writeTo="logFile" />
<logger minlevel="Trace" name="*" writeTo="logConsole" />
</rules>
</nlog>
测试调用:
private readonly static NLog.ILogger logger = NLog.LogManager.GetCurrentClassLogger(); //zhj-2019-03-28日志 //调用 logger.Trace("开始记录日志....");
logger.Info("socket Receive:" + servCmd);//zhj-2019-03-28记录日志
结果会显示在