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 ...
随机推荐
- linux下使用tar命令详解
解压语法:tar [主选项+辅选项] 文件或者目录 使用该命令时,主选项是必须要有的,它告诉tar要做什么事情,辅选项是辅助使用的,可以选用. 主选项: c 创建新的档案文件.如果用户想备份一个目录或 ...
- 学习FPGA,踏上一步台阶
学习FPGA的过程中,要想踏上一步台阶,需要注意一下几点: 时序约束的原因和使用方法,能熟练正确的应用最基本的时钟周期约束,时序例外约束,异步时钟域约束,同步复位的约束,高扇出约束. 清楚FPGA芯片 ...
- python 线程/进程模块
线程的基本使用: import threading # ###################### 1.线程的基本使用 def func(arg): print(arg) t = threading ...
- 网络网关TCP/IP
vmware中的4种网络连接模式 2008-11-13 11:11:21 分类: 系统运维 很多朋友都用vmware来测试不同的系统,我结合自己的经验谈一下对网络设置的理解,不对的地方请指正. bri ...
- 数据运算+-*/,比较运算符==!=,赋值运算,逻辑运算and,or,not,成员运算in,not in,身份运算is is not,位运算&|,运算符的优先级
取模就是返回余数. 取模的作用主要是来取奇偶数来用的,奇数干嘛,偶数干嘛. 比较运算符: 赋值运算: 逻辑运算: 赋值的时候可以多个变量同时赋值 成员运算: in就是在不在的意思. 身份运算: 位运算 ...
- 一次hadoop集群机器加内存的运维过程
由于前期的集群规划问题,导致当前Hadoop集群中的硬件并没有完全利用起来.当前机器的内存CPU比例为2G:1core,但一般的MapReduce任务(数据量处理比较大,逻辑较复杂)的MR两端都需要将 ...
- 20181122_C#中AOP_使用Unity实现AOP
一. 使用Unity的AOP实现 a) 整体项目截图: b) 添加Unity的Nuget包, 直接使用最新版就行, 需要添加两个 Unity 和 Unity.Interceptio ...
- js内置对象中获取时间的用法--通过date对象获取。
Date对象: var today = new Date(); //年份: var year = today.getFullYear(); //月份 var month = today.getMont ...
- Tkinter tkMessageBox
Tkinter tkMessageBox: tkMessageBox模块用于显示在您的应用程序的消息框.此模块提供了一个功能,您可以用它来显示适当的消息 tkMessageBox模块 ...
- 好记性不如烂笔头-Mysql查找如何判断字段是否包含某个字符串
好记性不如烂笔头-Mysql查找如何判断字段是否包含某个字符串 利用mysql 字符串函数 find_in_set(); SELECT * FROM users WHERE find_in_set(' ...