废话不多说直接进入正题。
1、在项目中加入Nlog的应用
安装后会出现两个文件
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"> <!-- See https://github.com/nlog/nlog/wiki/Configuration-file for information on customizing logging rules and outputs. --> <!-- 定义变量 projectName 项目名称 logDirectory 文件路径 --> <variable name="projectName" value="test"/> <variable name="logDirectory" value="F:/nlog/${projectName}/${shortdate}"/> <targets> <!-- 定义输出模板: type="File":这个记录方式为文件类型 fileName="${logDirectory}/All.log":表示输出到文件All.log中 layout="...":输出文件中错误的显示格式 ${logDirectory}:为上述定义的路径 ${longdate}:输出长日期 yyyy-MM-dd HH:mm:ss.ffff(例:2013-01-31 14:49:21.2120) ${level}:错误等级(由低到高为Trace,Debug,Info,Warn,Error,Fatal) ${newline}:输出 新的一行 ${stacktrace}:输出 堆栈信息 ${callsite:className=True:fileName=True:includeSourcePath=True:methodName=True}:输出 命名空间.类名.方法名(文件路径:行号) ${message}:输出错误信息--> <target xsi:type="File" name="logfiles" fileName="${logDirectory}/${shortdate}.log" layout="${longdate} ${level} ${message} ${stacktrace} ${callsite:className=True:fileName=True:includeSourcePath=True:methodName=True}${newline}" /> </targets> <rules> <!-- 定义输出日志: name="*":记录所有信息 minlevel="Trace":记录的最低错误级别为Trace writeTo="logfiles":日志写入logfiles的target中--> <logger name="*" minlevel="Trace" writeTo="logfiles" /> </rules> </nlog>
3、用简单的控制台程序测试一下
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using NLog; namespace NlogDemo { class Program { private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); static void Main(string[] args) { WriteLog(); } public static void WriteLog() { Logger.Log(LogLevel.Error, "nlog test error"); } } }
F5运行之后在我设置的路径下会有个以时间命名的log文件
日志内容
日志记录的内容与你在配置文件配置的fileName格式有关
4、以上的是文件的记录方式,下面我们来看数据库的记录方式
<?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"> <!-- See https://github.com/nlog/nlog/wiki/Configuration-file for information on customizing logging rules and outputs. --> <!-- 定义变量 projectName 项目名称 logDirectory 文件路径 --> <variable name="projectName" value="test"/> <variable name="logDirectory" value="F:/nlog/${projectName}/${shortdate}"/> <targets> <!-- 定义输出到MySQL中: type="database":这个记录方式是数据库 dbProvider="MySql.Data.MySqlClient":使用MySQL的连接方式 connectionString="":数据库的连接字符串 commandText="insert into Logs(CreateDate,LogLevel,CallSite,Massage,StackTrace) values (@CreateDate,@LogLevel,@CallSite,@Massage,@StackTrace)":insert语句 <target xsi:type="Database" name="logdatabase" dbProvider="MySql.Data.MySqlClient" connectionString="Server=127.0.0.1;Database=mytestdatabase;Uid=root;Pwd=123;" commandText="insert into Logs(create_time,loglevel,callsite,massage,stacktrace) values (@create_time,@loglevel,@callsite,@massage,@stacktrace)"> <!-- 对应到insert语句的参数的值--> <parameter name="create_time" layout="${longdate}" /> <parameter name="loglevel" layout="${level}" /> <parameter name="callsite" layout="${callsite:className=True:fileName=True:includeSourcePath=True:methodName=True}" /> <parameter name="massage" layout="${message}" /> <parameter name="stacktrace" layout="${stacktrace}" /> </target> </targets> <rules> <!-- 定义输出日志: name="*":记录所有信息 minlevel="Trace":记录的最低错误级别为Trace writeTo="logdatabase":日志写入数据库logdatabase中--> <logger name="*" minlevel="Trace" writeTo="logdatabase" /> </rules> </nlog>
sql语句
CREATE TABLE `mytestdatabase`.`Logs` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `create_time` DATETIME NOT NULL, `loglevel` VARCHAR(5) NOT NULL, `callsite` VARCHAR(5000) DEFAULT NULL, `massage` LONGTEXT, `stacktrace` VARCHAR(5000) DEFAULT NULL, PRIMARY KEY (`ID`) ) ENGINE=INNODB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
控制台程序还是上面的代码直接F5运行
数据库表中的数据
当然<targets>下面可以包含很多个target 我们也可以把日志文件同时写到文件和数据库表中
<?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"> <!-- See https://github.com/nlog/nlog/wiki/Configuration-file for information on customizing logging rules and outputs. --> <!-- 定义变量 projectName 项目名称 logDirectory 文件路径 --> <variable name="projectName" value="test"/> <variable name="logDirectory" value="F:/nlog/${projectName}/${shortdate}"/> <targets> <target xsi:type="File" name="logfiles" fileName="${logDirectory}/${shortdate}.log" layout="${longdate} ${level} ${message} ${stacktrace} ${callsite:className=True:fileName=True:includeSourcePath=True:methodName=True}${newline}" /> <target xsi:type="Database" name="logdatabase" dbProvider="MySql.Data.MySqlClient" connectionString="Server=127.0.0.1;Database=mytestdatabase;Uid=root;Pwd=123;" commandText="insert into Logs(create_time,loglevel,callsite,massage,stacktrace) values (@create_time,@loglevel,@callsite,@massage,@stacktrace)"> <parameter name="create_time" layout="${longdate}" /> <parameter name="loglevel" layout="${level}" /> <parameter name="callsite" layout="${callsite:className=True:fileName=True:includeSourcePath=True:methodName=True}" /> <parameter name="massage" layout="${message}" /> <parameter name="stacktrace" layout="${stacktrace}" /> </target> </targets> <rules> <logger name="*" minlevel="Trace" writeTo="logfiles,logdatabase" /> </rules> </nlog>
这里只是nlog日常简单的应用,更加详细的应用请参考
NLog Doc http://www.nlog-project.org/