Nlog- Application Logging in C#
当你在谷歌搜索 Application Loggin in C#,排在最前面的是这个 .NET Logging Tools and Libraries ,点击进去你会发现里面收录了不错的日记工具及文章。
这里我要介绍的是NLog。log4net 也是一个不错的选择。
NLog 官网:http://nlog-project.org/
NLog Github: https://github.com/NLog/NLog/wiki/Tutorial
先介绍使用方法:
1.安装
用Nuget 安装 NLog.Config。
Install-Package NLog.Config
这样他就会把需要的包都安装引入进到所安装的项目了。
2.配置
配置日记记录的形式,存储的地方等。安装好NLog之后,会自动生成一个NLog.config
打开它,里面其实会有详细的Demo教你怎么配置。
首先<target>, target 说明日志存放的地方。
<!--
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}" />
-->
<target xsi:type="File" name="debugfile" fileName="${basedir}/logs/${shortdate}.debug.html"
layout="Date: ${longdate} Logger: ${logger} Level: ${uppercase:${level}} <BR> Message: ${message} <BR><HR Size=1>"/>
<target xsi:type="File" name="errorfile" fileName="${basedir}/logs/${shortdate}.error.html"
layout="Date: ${longdate} Logger: ${logger} Level: ${uppercase:${level}} <BR> Message: ${message} <BR><HR Size=1>" />
其次是定义规则,日记也分为几个级别(Debug,Info,Warn,Error,Fatal 从左到右级别增加),不同的日记可以分开管理记录。
<rules>
<!-- add your logging rules here --> <!--
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" />
--> <logger name="*" minlevel="Debug" maxlevel="Debug" writeTo="debugfile" />
<logger name="*" minlevel="Info" writeTo="errorfile"/> </rules>
最后就是调用了。
using System.Web.Mvc;
using NLog; namespace App.Controllers
{
public class BaseController : Controller
{
protected Logger Log { get; private set; } protected BaseController()
{
Log = LogManager.GetLogger(GetType().FullName);
}
}
}
using System; namespace App.Controllers
{
public class UserController : BaseController
{
public void Index()
{
Log.Debug("This is Debug");
Log.Error(new Exception("除数不能为零"));
Log.Info("使用起来简单吧");
}
}
}
出来的效果:
2016-06-13.debug.html
Date: -- ::17.5376 Logger: App.Controllers.UserController Level: DEBUG <BR> Message: This is Debug <BR><HR Size=>
2016-06-13.error.html
Date: -- ::17.5486 Logger: App.Controllers.UserController Level: ERROR <BR> Message: System.Exception: 除数不能为零 <BR><HR Size=>
Date: -- ::17.5486 Logger: App.Controllers.UserController Level: INFO <BR> Message: 使用起来简单吧 <BR><HR Size=>
上面target 只有 file。其实还有很多种形式的输出,这个要在下一篇再写了。
Nlog- Application Logging in C#的更多相关文章
- qt application logging
“AnalysisPtsDataTool201905.exe”(Win32): 已加载“F:\OpencvProject\ZY-Project\x64\Debug\AnalysisPtsDataToo ...
- [转]ASP.NET Core 开发-Logging 使用NLog 写日志文件
本文转自:http://www.cnblogs.com/Leo_wl/p/5561812.html ASP.NET Core 开发-Logging 使用NLog 写日志文件. NLog 可以适用于 . ...
- Core 开发-Logging 使用NLog
ASP.NET Core 开发-Logging 使用NLog 写日志文件 ASP.NET Core 开发-Logging 使用NLog 写日志文件. NLog 可以适用于 .NET Core 和 ...
- ASP.NET Core 开发-Logging 使用NLog 写日志文件
ASP.NET Core 开发-Logging 使用NLog 写日志文件. NLog 可以适用于 .NET Core 和 ASP.NET Core . ASP.NET Core已经内置了日志支持,可以 ...
- NLog在.NET Core Console Apps中的简单应用
什么是NLog? NLog is a free logging platform for .NET with rich log routing and management capabilities. ...
- Nlog 配置总结
Writes log messages to one or more files. Since NLog 4.3 the ${basedir} isn't needed anymore for rel ...
- ASP.NET Core使用NLog记录日志到Microsoft Sql Server
在之前的文章中介绍了如何在ASP.NET Core使用NLog,本文为您介绍在ASP.NET Core使用NLog记录到Microsoft Sql Server 1.我们需要添加依赖: NLog.We ...
- .Net Core 使用NLog记录日志到文件和数据库
NLog 记录日志是微软官方推荐使用. 接下来,通过配置日志记录到文件和Sql Server数据库. 第一步:首先添加包NLog.Config (可通过微软添加包命令Install-Package 包 ...
- [转]Setting the NLog database connection string in the ASP.NET Core appsettings.json
本文转自:https://damienbod.com/2016/09/22/setting-the-nlog-database-connection-string-in-the-asp-net-cor ...
- Logging configuration
The Play logger is built on Log4j. Since most Java libraries use Log4j or a wrapper able to use Log4 ...
随机推荐
- Win10怎么以管理员身份运行CMD命令提示符
[方法一] 我们可以在Windows10系统的开始菜单上,单击鼠标右键,这时候出现的菜单中,我们选择命令提示符(管理员)点击打开这样即可. 2 这样打开以后,大家可以看到命令提示符界面中显示管理员:命 ...
- Windows7下搭建Python2.7环境
机器: Windows7_x86_64 步骤: 1.下载Python2.7 下载地址:https://www.python.org/downloads/ 2.安装Python2.7 双击安装包,安装过 ...
- 将子类对象引用赋值给超类对象 JAVA 编译时多态性
将子类对象引用赋值给超类对象 JAVA 编译时多态性(转) (2012-05-10 11:24:05) 转载▼ 标签: 杂谈 分类: 也无晴_soft 1.通过将子类对象引用赋值给超类对象引用变量来实 ...
- Cocos2d-html5帧动画
单独获取plist里面一个文件: cc.SpriteFrameCache.getInstance().addSpriteFrames(s_test_plist); var spriteTest2 = ...
- windows server 2008 配置DNS服务器与IIS
0x00: 总结这个星期在学校学的. 0x01安装: 首先你得安装好windows server 2008 然后在添加角色->安装IIS和DNS服务器 勾选好你要安装的. 安装-> 根据老 ...
- cocos2dx 3.6源码分析之文件路径
cocos2dx中资源文件都放在Resources目录中,编译后会自动复制到exe所在的目录中. 核心类是FileUtils类,一个单例类. 三个重要的函数 void addSearchPath(co ...
- cocostudio使用plist创建序列帧动画图片名称序列错乱的问题
cocostudio 版本v2.2.9 用texturePacker将动画帧打包成一个plist和一张png,将plist拖入cocostudio中的资源栏中. 如图所示,plist里面的图片顺序是乱 ...
- Spring之导入和混合配置
在典型的Spring应用中,我们可能会同时使用自动化和显式配置.即便你更喜欢通过JavaConfig实现显式配置,但有的时候XML却是最佳的方案.幸好在Spring中,这些配置方案都不是互斥的.你尽可 ...
- Apache加载curl_init 失败 php_curl.dll
Call to undefined function curl_init php.ini curl_init extension=php_curl.dll xampp\php\php.ini 2018 ...
- avalon1.3的新特性预览
avalon1.2的性能优化风暴很快就告一段落,入职也快一个月了,许多乱七八糟的事也少了下来,估计未来一个月会有许多好东呈现给大家. 首先是一个性能检测工具.由于MVVM是将原本由人脑干的事,转到各种 ...