NLog 配置与使用
有段时间没写博客了,过年放假,一直在弄CMS。什么都自己写了一遍,今天写写NLog,之前一用的log4net,感觉配置起来还是有些麻烦。
NuGet 添加组件

配置 NLog.config
<?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="true"
internalLogLevel="Debug"
internalLogFile="c:\NLog\log.txt">
<targets async="true">
<default-wrapper xsi:type="BufferingWrapper" bufferSize="100"/>
<!-- write log message to console -->
<target xsi:type="Console" name="console"
layout="
${newline}时间: ${longdate}
${newline}来源: ${callsite}
${newline}等级: ${level}
${newline}信息: ${message}
${newline}堆栈: ${event-context:item=exception} ${stacktrace}
${newline}${newline}-----------------------------------------------------------" />
<!-- write log message to file -->
<target xsi:type="File" name="file" fileName="${basedir}/Logs/${date:format=yyyy}/${date:format=MM}/${level}/${shortdate}.txt"
layout="
${newline}时间: ${longdate}
${newline}来源: ${callsite}
${newline}等级: ${level}
${newline}信息: ${message}
${newline}堆栈: ${event-context:item=exception} ${stacktrace}
${newline}${newline}-----------------------------------------------------------" />
<!-- write log message to database -->
<target xsi:type="Database" name="database" connectionstring="Data Source=DESKTOP-1COGQ4S;Initial Catalog=MiaoZhanCMS_db;Integrated Security=True;User ID=sa;Password=wangcong;">
<!-- SQL command to be executed for each entry -->
<commandText>
INSERT INTO SystemLog(UserName,UserId,OperationType,MenuName,Action,Contents,IP)
VALUES (@userName, @userId, @operationType, @menuName, @action, @contents, @IP);
</commandText>
<!-- parameters for the command -->
<!--日记来源-->
<!--<parameter name="@origin" layout="${callsite}" />
--><!--日志级别--><!--
<parameter name="@levels" layout="${level}" />
--><!--异常信息--><!--
<parameter name="@message" layout="${message}" />
--><!--堆栈信息--><!--
<parameter name="@stacktrace" layout="${stacktrace}" />-->
<parameter name="@userName" layout="${event-context:item=userName}" />
<parameter name="@userId" layout="${event-context:item=userId}" />
<parameter name="@operationType" layout="${event-context:item=operationType}" />
<parameter name="@menuName" layout="${event-context:item=menuName}" />
<parameter name="@action" layout="${event-context:item=action}" />
<parameter name="@contents" layout="${event-context:item=contents}" />
<parameter name="@IP" layout="${event-context:item=IP}" />
</target>
<!-- write log message to mail -->
<!--<target xsi:type="Mail" name="infoMail"
smtpServer="smtp.qq.com"
smtpPort="25"
smtpAuthentication="Basic"
smtpUserName="邮箱账号"
smtpPassword="邮箱密码"
enableSsl="true"
addNewLines="true"
from="发送邮箱"
to="接收邮箱"
subject="xx系统错误日志"
header="======================================="
body="
${newline}时间: ${longdate}
${newline}来源: ${callsite}
${newline}等级: ${level}
${newline}信息: ${message}
${newline}堆栈: ${event-context:item=exception} ${stacktrace}"
footer="=======================================" />-->
</targets>
<rules>
<logger name="*" writeTo="console" />
<logger name="*" writeTo="file" />
<logger name="*" writeTo="database"/>
<!--<logger name="*" minlevel="Error" writeTo="infoMail" />-->
</rules>
</nlog>
配置文件包括如下,如果需要扩展,可以自己方法。
此配置是异步写入,按年、月、日、分成级别日志。
- 写入文件
- 写入数据库
- 控制台显示
- 错误邮件提示
总结
我主要是用来是系统日志,配置方便简洁,效率也很高,就是这么酸爽。
NLog 配置与使用的更多相关文章
- ASP.NET Core根据环境切换NLog配置
1.新建NLog配置文件,名称分别为nlog.config和nlog.debug.config <?xml version="1.0"?> <nlog xmlns ...
- Nlog配置
初次使用nlog,里里外外找了好久,终于搞会了. 使用nlog建日志输出到txt文件.数据库.邮件 nlog配置,如图 码云dome
- NLog 配置
之前我介绍过如何使用log4net来记录日志,但最近喜欢上了另一个简单好用的日志框架NLog. 关于NLog和log4net的比较这里就不多讨论了,感兴趣的朋友可以参看.NET日志工具介绍和log4n ...
- Nlog配置实例
彩色Console target <?xml version="1.0" encoding="utf-8" ?> <nlog xmlns= ...
- Nlog 配置总结
Writes log messages to one or more files. Since NLog 4.3 the ${basedir} isn't needed anymore for rel ...
- NLog配置分享
新建一个文件命名为NLog.Config,然后添加如下代码 <?xml version="1.0" encoding="utf-8" ?> < ...
- NLog配置JsonLayout中文输出为unicode问题
日志输出现要改为json格式,网上查询layout配置为JsonLayout就可以了,结果发现输出中文为unicode编码,看很多文章说配置encode="false"就可以了,结 ...
- 常用NLog配置
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSe ...
- .net core webapi +ddd(领域驱动)+nlog配置+swagger配置 学习笔记(1)
搭建一个.net core webapi项目 在开始之前,请先安装最新版本的VS2017,以及最新的.net core 2.1. 首先创建一个Asp.Net Core Web应用程序 这个应用程序是 ...
随机推荐
- 快速掌握activity的生命周期
activity的生命周期不管是在面试还是在工作中我们都会经常遇到,这当然也是非常基础的,基础也很重要哦,学会activity的生命周期对我们以后开发更健壮的程序会有很大帮助.下面来看一下Activi ...
- iOS - OC 与 C 互相操作
前言 在 Objective-C 语言中,我们可以使用 C.Swift 语言编写代码,我们可以导入任意用 C.Swift 写的 Cocoa 平台框架.C 框架或 Swift 类库. 1.在 C 中使用 ...
- LaTeX多文件编译的方法总结
LaTeX多文件编译的方法总结 在编写LaTeX文档的时候,由于文档的section较多,或者section的编写时间各不相同,我们可能碰到如下问题: 1.由于想分开编写各个section 2.pre ...
- Creating the Help Page in ASP.NET Web API
Introduction In this article we will define the process of creating the help page in the ASP .NET We ...
- 怎么使用 ab.exe 测试多个url。 how to use ab.exe test many url
from a commandline in windows: for /F %q in (list.txt) DO ab -n 1000 https://test.com/search?%q I ...
- java中代理,静态代理,动态代理以及spring aop代理方式,实现原理统一汇总
若代理类在程序运行前就已经存在,那么这种代理方式被成为 静态代理 ,这种情况下的代理类通常都是我们在Java代码中定义的. 通常情况下, 静态代理中的代理类和委托类会实现同一接口或是派生自相同的父类. ...
- GPS轨迹数据集免费下载资源整理
https://blog.csdn.net/liangyihuai/article/details/58335510
- Win8.1的IE11无法打开,必须使用管理员身份运行
不知道大家有没有遇到这种情况,在毫不知情的情况下 IE11 突然打不开了,必须要用管理员身份运行才可以打开,而且重置浏览器这个方法也不奏效. 今天本人也遇到了,上网查找发现是注册表权限的问题,原因尚不 ...
- 解决:IIS APPPOOL\DefaultAppPool 登录失败的问题
为大家介绍下解决IIS APPPOOL\DefaultAppPool 登录失败的方法,有遇到类似问题的朋友参考下. 添加ASP.NET网站时,选择添加"添加应用程序"连接sql s ...
- FPGA三分频,五分频,奇数分频
我们在做FPGA设计时,有时会用到时钟频率奇数分频的频率,例如笔者FPGA的晶振为50M,当我们需要10M的时钟时,一种方式可以使用DCM或PLL获取,系统会内部分频到10M,但其实VERILOG内部 ...