1.从Packet Mananger中安装NLog
NuGet\Install-Package NLog -Version 5.3.2
2.在项目名右键新建一个应用程序配置文件NLog.config,输入以下内容,这个文件最后需要copy到可执行文件同目录
<?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>
<!--fileName是文件存放路径和名字-->
<target name="logfile" xsi:type="File" fileName="${basedir}/${hostname}_${date:format=yyyy-MM-dd}.txt"
layout="${longdate}|${level:uppercase=true}|${logger}|${threadid}-${threadname}|${message}|${exception:format=tostring}"
/>"
<target name="logconsole" xsi:type="Console"
layout="${longdate}|${level:uppercase=true}|${logger}|${threadid}|${message}|${exception:format=tostring}"
/>
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="logconsole" />
<logger name="*" minlevel="Debug" writeTo="logfile" />
</rules>
</nlog>
2.在程序中使用
// 新建一个对象
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
// 在代码中log信息
logger.Info("This is a logger info");
//析构函数中释放
NLog.LogManager.Shutdown();