有段时间没写博客了,过年放假,一直在弄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>

配置文件包括如下,如果需要扩展,可以自己方法。

此配置是异步写入,按年、月、日、分成级别日志。

  1. 写入文件
  2. 写入数据库
  3. 控制台显示
  4. 错误邮件提示

总结

我主要是用来是系统日志,配置方便简洁,效率也很高,就是这么酸爽。

NLog 配置与使用的更多相关文章

  1. ASP.NET Core根据环境切换NLog配置

    1.新建NLog配置文件,名称分别为nlog.config和nlog.debug.config <?xml version="1.0"?> <nlog xmlns ...

  2. Nlog配置

    初次使用nlog,里里外外找了好久,终于搞会了. 使用nlog建日志输出到txt文件.数据库.邮件 nlog配置,如图 码云dome

  3. NLog 配置

    之前我介绍过如何使用log4net来记录日志,但最近喜欢上了另一个简单好用的日志框架NLog. 关于NLog和log4net的比较这里就不多讨论了,感兴趣的朋友可以参看.NET日志工具介绍和log4n ...

  4. Nlog配置实例

      彩色Console target <?xml version="1.0" encoding="utf-8" ?> <nlog xmlns= ...

  5. Nlog 配置总结

    Writes log messages to one or more files. Since NLog 4.3 the ${basedir} isn't needed anymore for rel ...

  6. NLog配置分享

    新建一个文件命名为NLog.Config,然后添加如下代码 <?xml version="1.0" encoding="utf-8" ?> < ...

  7. NLog配置JsonLayout中文输出为unicode问题

    日志输出现要改为json格式,网上查询layout配置为JsonLayout就可以了,结果发现输出中文为unicode编码,看很多文章说配置encode="false"就可以了,结 ...

  8. 常用NLog配置

    <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSe ...

  9. .net core webapi +ddd(领域驱动)+nlog配置+swagger配置 学习笔记(1)

    搭建一个.net core webapi项目  在开始之前,请先安装最新版本的VS2017,以及最新的.net core 2.1. 首先创建一个Asp.Net Core Web应用程序 这个应用程序是 ...

随机推荐

  1. maven springmvc spring data jpa hibernate sqlserver demo

    搭建费了半天费,各种报错,缺少各种jar包,不兼容等,给那些没弄过的一个参考. 点击我下载

  2. python练习笔记——编写一个装饰器,模拟登录的简单验证

    编写一个装饰器,模拟登录的简单验证(至验证用户名和密码是否正确) 如果用户名为 root 密码为 123则正确,否则不正确.如果验证不通过则不执行被修饰函数 #编写一个装饰器,模拟登录的简单验证 #只 ...

  3. Jmeter时间函数工具(参考)

    __time : 获取时间戳.格式化时间 ${__time(yyyy-MM-dd HH:mm:ss:SSS,time)}  :格式化生成时间格式 2018-06-01 11:08:23:635 ${_ ...

  4. [转载]Class-AB Amplifier 笔记

    Class-AB Amplifier 笔记 Reading Notes from Mikko Loikkanen “Design and Compensation of High Performanc ...

  5. CSS2中的伪类与伪元素

    CSS 伪类用于向某些选择器添加特殊的效果. 我们最常见的就是有超链接的时候,向下面这样 a:link {color: #FF0000} /* 未访问的链接 */ a:visited {color: ...

  6. linux分享一:进程全攻略--守护进程(服务)

    概括: 进程是程序的运行实例.进程对应一个唯一的进程PID, 统一程序的多个实例可以同时运行,他们的pid互不相同. 进程一般分为交互进程.批处理进程和守护进程(daemons)三类 一:什么是守护进 ...

  7. uva 10537 Toll! Revisited(优先队列优化dijstra及变形)

    Toll! Revisited 大致题意:有两种节点,一种是大写字母,一种是小写字母. 首先输入m条边.当经过小写字母时须要付一单位的过路费.当经过大写字母时,要付当前財务的1/20做过路费. 问在起 ...

  8. QThread 实用技巧、误区----但文档中没有提到

    本文主要内容: 在任务一中,用 四 种方式实现:点击界面按钮,开线程运行一段程序,结果显示在一个Label上.1. 用不正确的方式得到看似正确的结果2. 用Qt Manual 和 例子中使用的方法3. ...

  9. MySQL PLSQL Demo - 003.静态游标

    drop procedure if exists p_hello_world; create procedure p_hello_world() begin declare id integer; ) ...

  10. 用C++画光(二)——矩形

    在上篇文章的基础上,做了许多调整,修复了许多BUG.在解决bug的过程中,我逐渐领悟到一个要领:枯燥地一步步调试太痛苦了,找不到问题的根源!所以我选择将中间结果打到图片上.如: (注意,里面的点是我随 ...