本文主要是介绍NLog 配置文件及简单使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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();
这篇关于NLog 配置文件及简单使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!