需求:c#日志打印工具类,方便定位问题,调试程序,之前使用log4net工具类,发现该打印日志影响form启动效率,
于是自己写了一个打印日志类,不是很专业,现在使用NLog日志打印类,还不错,推荐小伙伴们使用~
配置使用:
1.Nuget方式安装:
2.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="100"
archiveEvery="Day"
archiveNumbering="Date"
archiveDateFormat="yyyyMMdd"
archiveFileName="${varDataFolderPath}/archives/log.{#}.txt"
fileName="${varDataFolderPath}/nlog-${shortdate}.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>
3.NLog.config 属性更改
结果:
PS:当前配置是按天为单位输出日志文件,没有当前天日志文件,当有日志输出时,创建当前天日志文件~