C# Nlog 日志框架使用介绍

NLog是一个基于.NET平台编写的类库,我们可以使用NLog在应用程序中添加极为完善的跟踪调试代码。
NLog是一个简单灵活的.NET日志记录类库。通过使用NLog,我们可以在任何一种.NET语言中输出带有上下文的(contextual information)调试诊断信息,根据喜好配置其表现样式之后发送到一个或多个输出目标(target)中。

快速安装

在软件包管理器控制台中使用GUI或以下命令:
1.安装Nlog

Install-Package Nlog
2.安装Nlog.Config

Install-Package Nlog.Config


## 快速配置
打开目录中得Nlog.Config文件, 可以注意到, XML文件中有详细说明, rules区允许添加用户自定义得路由规则, targets则用于配置一些输出目标, 如下:
<?xml version="1.0" encoding="utf-8" ?>

<!--
add your targets here
See https://github.com/nlog/NLog/wiki/Targets for possible targets.
See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
-->

<!--
Write events to a file with the date in the filename.
<target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
        layout="${longdate} ${uppercase:${level}} ${message}" />
-->
<!--
Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace)  to "f"
<logger name="*" minlevel="Debug" writeTo="f" />
-->
上一篇:c#日志工具类


下一篇:初赛注意集锦