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 ...
随机推荐
- GOF23设计模式之状态模式(state)
一.状态模式概述 用于解决系统中复杂对象的状态转换以及不同状态下行为的封装问题. 结构: (1)Context 环境类 环境类中维护一个 State 对象,它定义了当前的状态. (2)State ...
- 高效网页去重算法-SimHash
记得以前有人问过我,网页去重算法有哪些,我不假思索的说出了余弦向量相似度匹配,但如果是数十亿级别的网页去重呢?这下糟糕了,因为每两个网页都需要计算一次向量内积,查重效率太低了!我当时就想:论查找效率肯 ...
- Window app遇到的问题
Windows下两个应用之间进行UDP通讯,必须要先互相发送过数据,才能收到其他的数据.解决方法: <Capabilities> <Capability Name="int ...
- Java多线程面试问题集锦
参看:http://www.importnew.com/1428.html
- 深入浅出 Java Concurrency (12): 锁机制 part 7 信号量(Semaphore)
Semaphore 是一个计数信号量.从概念上讲,信号量维护了一个许可集.如有必要,在许可可用前会阻塞每一个 acquire(),然后再获取该许可.每个 release() 添加一个许可,从而可能 ...
- .net Reactor之限指定设备使用
.net Reactor之license限指定设备使用 上一篇(https://www.cnblogs.com/s313139232/p/9908400.html)中记录了.net Reactor对d ...
- 佛祖保佑、永无BUG!!!
/* _ooOoo_ o8888888o 88" . "88 (| -_- |) O\ = /O ____/`---'\____ .' \\| |// `. / \\||| : | ...
- MyBatis 学习记录7 一个Bug引发的思考
主题 这次学习MyBatis的主题我想记录一个使用起来可能会遇到,但是没有经验的话很不好解决的BUG,在特定情况下很容易发生. 异常 java.lang.IllegalArgumentExceptio ...
- python:一个轻松的递归逻辑
#递归 age = 10 def dig(n): global age#函数dig引用全局变量age age += 2 n -= 1 if n != 1:#如果满足条件,则调用本身 dig(n) di ...
- 常用sql语句备份
1.查看数据库中所有的存储过程 use [DataBase_Name] go SELECT * FROM sys.all_objects WHERE ([type] = 'P' OR [type] = ...