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" >
<targets async="true" maxarchivefiles="2">
<target name="log_file" xsi:type="File"
fileName="${basedir}/logs/gadget.${date:format=yyyyMMdd}.log"
layout="[${longdate}|${level}|${message}|${exception}]"/>
<target name="log_file2" xsi:type="File" fileName="${basedir}/logs/gadget.${date:format=yyyyMMdd}.log"
layout="[----------------------------${newline}${date} ${appdomain} ${logger} ${level}${newline}${message}${newline}${exception}]"/>
</targets>
<rules>
<logger name="*" minlevel="trace" writeTo="log_file"></logger>
</rules>
</nlog>
NuGet packages
<PackageReference Include="NLog.Windows.Forms" Version="5.2.0" />
调用
//Logger Logger { get { return LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger(); } }
Logger Logger { get { return LogManager.Setup().GetCurrentClassLogger(); } }
logger.Info("MainForm"); logger.Error("MainForm"); logger.Debug("MainForm"); logger.Trace("MainForm"); logger.Warn("MainForm");
该文展示了如何配置NLog以实现异步日志记录,创建两个不同的日志文件格式,并设置最大归档文件数。通过NuGet包NLog.Windows.Forms引入,文中还提供了如何在C#代码中调用NLog进行不同级别的日志记录。
1316

被折叠的 条评论
为什么被折叠?



