NLog是一个后来发展起来的日志功能,比起Log4net要年轻许多,但是功能确实非常好用,并且配置简单。
1、安装Nlog 和Nlog configuration
点击安装下边的NLog.Config就行了,NLog会一起安装成功。
2、配置文件
安装完之后项目里边多了(nllog.config属性设置=》复制到生成目录:始终复制,生成操作:内容)
配置内容写在这个里边。主要用到两个标签:
Target:这个是主要用来配置日志输出的相关参数的,比如输出到文件还是数据库,输出的文件名称是什么,内容格式是什么等。
Rules:输出规则,定义根据不同的日志级别输出到不同的地方,比如Info类型的输出到文件,Error类型的通过邮件发送处理等。
详细的配置内容参考官方文档:https://github.com/nlog/NLog/wiki/Configuration-file
贴一个我自己写的比较简单的配置内容:
<?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"
autoReload="true"
throwExceptions="false"
internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log" >
<!--
See http://nlog-project.org/wiki/Configuration_file
for information on customizing logging rules and outputs.
-->
<extensions>
<!--
引用工具类存放的程序集
-->
<add assembly="Demo.Common"/>
</extensions>
<targets async="true">
<!--文本(保存在项目的根目录下logs文件)-->
<target xsi:type="File" encoding="utf-8" name="File" fileName="${basedir}/logs/${level}${shortdate}.log" layout="${longdate}|${uppercase:${level}}${newline}${stacktrace}${newline}${message}${newline}${onexception:${exception:format=tostring} " />
<!--控制台-->
<target name="Console" xsi:type="Console" layout="${longdate}|${uppercase:${level}}${newline}${stacktrace}${newline}${message}${newline}${onexception:${exception:format=tostring} " />
</targets>
<rules>
<!-- add your logging rules here -->
<logger name="*" minlevel="Trace" writeTo="File"/>
</rules>
</nlog>
3、工具类
public static class LogHelper
{
private static readonly ILog Logger;
static LogHelper()
{
//默认NLog
Logger = new Logging.Impls.NLog();
}
#region 系统跟踪日志
[DebuggerStepThrough]
public static void Trace(string message)
{
Logger.Trace(message);
}
[DebuggerStepThrough]
public static void TraceFormat(string format, params object[] args)
{
Logger.Trace(string.Format(format, args));
}
[DebuggerStepThrough]
public static void Debug(string message)
{
Logger.Debug(message);
}
[DebuggerStepThrough]
public static void Info(string message)
{
Logger.Info(message);
}
[DebuggerStepThrough]
public static void InfoFormat(string format, params object[] args)
{
Logger.Info(string.Format(format, args));
}
[DebuggerStepThrough]
public static void InfoKeyword(string message, params object[] keywords)
{
Logger.Info(message, keywords);
}
[DebuggerStepThrough]
public static void Warning(string message)
{
Logger.Warning(message);
}
[DebuggerStepThrough]
public static void WarningFormat(string format, params object[] args)
{
Logger.Warning(string.Format(format, args));
}
[DebuggerStepThrough]
public static void Error(string message)
{
Logger.Error(message);
}
[DebuggerStepThrough]
public static void ErrorFormat(string format, params object[] args)
{
Logger.Error(string.Format(format, args));
}
public static void ErrorKeyword(string message, params object[] keywords)
{
Logger.Error(message, keywords);
}
[DebuggerStepThrough]
public static void Exception(Exception exception, params object[] keywords)
{
Logger.Exception(exception, null, keywords);
}
#endregion
}
4、效果
全局捕获异常,并输出到日志:
我们在项目中找到了生成的日志和对应的错误信息,这样基本的日志功能就实现了。