NLog日志记录
配置NLog
在ASP.NET项目中搜索的目录包括:
- 标准的web程序配置文件web.config
- 和web.config在同一目录下的web.nlog文件
- 程序目录下的NLog.config文件
- NLog.dll所在目录下的NLog.dll.nlog文件 (在Nlog没有导入GAC情况下)
- 如果定义了NLOG_GLOBAL_CONFIG_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"xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"autoReload="true"throwExceptions="false"internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log" ><!-- optional, add some variabeleshttps://github.com/nlog/NLog/wiki/Configuration-file#variables--><variable name="myvar" value="myvalue"/><!--See https://github.com/nlog/nlog/wiki/Configuration-filefor information on customizing logging rules and outputs.--><targets><!--add your targets hereSee 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="dbg" fileName="${basedir}/logs/Debug${shortdate}.log"layout="${longdate} ${uppercase:${level}} ${message}" /><target xsi:type="File" name="Eor" fileName="${basedir}/logs/Error${shortdate}.log"layout="${longdate} ${uppercase:${level}} ${message}" /><target xsi:type="File" name="Tre" fileName="${basedir}/logs/Trace${shortdate}.log"layout="${longdate} ${uppercase:${level}} ${message}" /><target xsi:type="File" name="War" fileName="${basedir}/logs/Warn${shortdate}.log"layout="${longdate} ${uppercase:${level}} ${message}" /></targets><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" writeTo="dbg" /><logger name="*" minlevel="Error" writeTo="Eor" /><logger name="*" minlevel="Trace" writeTo="Tre" /><logger name="*" minlevel="Warn" writeTo="War" /></rules></nlog>
子元素的配置:
- <targets /> – defines log targets/outputs ,声明目标
- <rules /> – defines log routing rules ,声明规则
- <extensions /> – loads NLog extensions from the *.dll file 加载dll扩展(其实我不懂,没用过)
- <include /> – includes external configuration file 包含外部配置文件
- <variable /> – sets the value of a configuration variable 为配置变量赋值
路由规则:
- name - 日志源/记录者的名字 (允许使用通配符*)
- minlevel - 该规则所匹配日志范围的最低级别
- maxlevel - 该规则所匹配日志范围的最高级别
- level - 该规则所匹配的单一日志级别
- levels - 该规则所匹配的一系列日志级别,由逗号分隔。
- writeTo - 规则匹配时日志应该被写入的一系列目标,由逗号分隔。
- final - 标记当前规则为最后一个规则。其后的规则即时匹配也不会被运行。
如:
- <logger name="Name.Space.Class1" minlevel="Debug" writeTo="f1" /> - 名字空间Name.Space下的Class1这个类的所有级别等于或者高于Debug的日志信息都写入到“f1”这个目标里。
- <logger name="Name.Space.Class1" levels="Debug,Error" writeTo="f1" /> -名字空间Name.Space下的Class1这个类的所有级别等于Debug或Error的日志信息都写入到“f1”这个目标里。
- <logger name="Name.Space.*" writeTo="f3,f4" /> -名字空间Name.Space下所有类的所有级别的日志信息都写入到“f3”和“f4”这两个目标里。
- <logger name="Name.Space.*" minlevel="Debug" maxlevel="Error" final="true" /> - 名字空间Name.Space下所有类的、级别在Debug和Error之间的(包括Debug,Info,Warn,Error) 日志信息都不会被记录(因为这条规则没有定义writeTo),同时其它后续规则也都会被忽略(因为这里设置了final="true")。
配置NLog到App.config文件
<?xml version="1.0" encoding="utf-8" ?><configuration><!--这里的ConfigSections 必须要处于根节点下的第一个节点,至于原因目前还不清楚--><configSections><section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/></configSections><nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><!-- See http://nlog-project.org/wiki/Configuration_file for information on customizing logging rules and outputs. --><targets><target xsi:type="File" name="f" fileName="${basedir}/APP_Data/logs/${shortdate}.log" layout="${longdate} ${uppercase:${level}} ${message}" /></targets><rules><logger name="*" minlevel="Trace" writeTo="f" /></rules></nlog><startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /></startup></configuration>
在代码中使用Log记录日志信息
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();static void Main(string[] args){//Logger logger = LogManager.GetLogger("MyClassName");logger.Debug("Test Logger Message");}
附件列表
NLog日志记录的更多相关文章
- .NET Core 使用NLog日志记录
前言 每个项目都会需要使用到日志功能,这对于项目上线后 出现的bug异常,能及时定位和便于后期错误分析.那我们今天来看看在.NET Core中如何使用NLog日志. NLog 什么是NLog呢? NL ...
- .Net Core Nlog日志记录到MySql
前段时间想要实现这个功能网上找了很多资料,现在整理一下发布出来,希望给大家一点帮助. 首先是依赖项的选择: 关于NLog版本不是最新是因为最新版本有点问题我试了试不支持,所以选了这几个版本,MySql ...
- Net Core平台灵活简单的日志记录框架NLog+Mysql组合初体验
Net Core平台灵活简单的日志记录框架NLog初体验 前几天分享的"[Net Core集成Exceptionless分布式日志功能以及全局异常过滤][https://www.cnblog ...
- Web APi之异常处理(Exception)以及日志记录(NLog)(十六)
前言 上一篇文章我们介绍了关于日志记录用的是Log4net,确实也很挺强大,但是别忘了我们.NET有专属于我们的日志框架,那就是NLog,相对于Log4net而言,NLog可以说也是一个很好的记录日志 ...
- Asp.Net Core 2.0 项目实战(9) 日志记录,基于Nlog或Microsoft.Extensions.Logging的实现及调用实例
本文目录 1. Net下日志记录 2. NLog的使用 2.1 添加nuget引用NLog.Web.AspNetCore 2.2 配置文件设置 2.3 依赖配置及调用 ...
- 封装一个基于NLog+NLog.Mongo的日志记录工具类LogUtil
封装一个基于NLog+NLog.Mongo的日志记录工具类LogUtil,代码比较简单,主要是把MongoTarget的配置.FileTarget的配置集成到类中,同时利用缓存依赖来判断是否需要重新创 ...
- Net Core平台灵活简单的日志记录框架NLog+SqlServer初体验
Net Core平台灵活简单的日志记录框架NLog+SqlServer初体验 前几天分享的"[Net Core平台灵活简单的日志记录框架NLog+Mysql组合初体验][http://www ...
- 封装一个基于NLog+NLog.Mongo的日志记录工具类LogUtil,nloglogutil
封装一个基于NLog+NLog.Mongo的日志记录工具类LogUtil,代码比较简单,主要是把MongoTarget的配置.FileTarget的配置集成到类中,同时利用缓存依赖来判断是否需要重新创 ...
- ASP.NET Core使用Elasticsearch记录NLog日志
ASP.NET Core使用Elasticsearch记录NLog日志 1.新建一个 ASP.NET Core项目 2.安装Nuge包 运行:Install-Package NLog.Web.AspN ...
随机推荐
- (转载)activity外部调用startActivity的new task异常解析
activity外部调用startActivity的new task异常解析 泡在网上的日子 / 文 发表于2013-09-07 12:45 第1314次阅读 异常,android,activity ...
- MySQL 5.6 Reference Manual-14.2 InnoDB Concepts and Architecture
14.2 InnoDB Concepts and Architecture 14.2.1 MySQL and the ACID Model 14.2.2 InnoDB Multi-Versioning ...
- R 连接DB2数据库,并制作词图
#写在前面的话:此教程主要是用R连接了DB2数据库,并进行文本分析,制作了词图 #教程为markdown编写 ---title: "网站留言分析"output: html_docu ...
- 使用JDK和axis2发布webservice
最近使用webservice进行远程调用一直很火,自从JDK1.6版本发布后,发布一个webservice项目变得更加简单了 笔者由于工作的需要针对JDK和axis2如何发布webservice做过相 ...
- mwArray与C++接口
1.Matlab调用C++:http://blog.csdn.net/zouxy09/article/details/20553007 摘录下效果图: 2.mwArray类操作:http://blog ...
- JS 马托货物
大马驮2石粮食,中马驮1石粮食,两头小马驮一石粮食,要用100匹马,驮100石粮食,该如何调配? <!DOCTYPE html> <html> <head> < ...
- HH的项链 树状数组动态维护前缀
#include<cstdio> #include<algorithm> #include<cstring> using namespace std; const ...
- ojdbc14:11.2.0.1.0出错
首先在相关目录下找到你的ojdbc14的包:比如 我的出错问题:(之前是11.2.0.1.0,后改为10.2.0.1.0) 我的包路径:C:\my_java\maven_repository\com\ ...
- 安装sublimeServer插件
1.安装目的 做练习在谷歌浏览器中遇到报错信息:axios.min.js:8 Failed to load file:///E:/%E8%87%AA%E5%AD%A6/vue%E5%AD%A6%E4% ...
- Linux进程地址管理之mm_struct
FROM : http://www.cnblogs.com/Rofael/archive/2013/04/13/3019153.html Linux对于内存的管理涉及到非常多的方面,这篇文章首先从对进 ...