有段时间没写博客了,过年放假,一直在弄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. vi/vim 计算搜寻关键字数量

    http://hi.baidu.com/xletian/blog/item/f19962061a9a506c020881dc.html 在看过 vim 的全域指令和 search 指令之後,你会不会也 ...

  2. vi/vim 查找替换使用方法

    http://wzgyantai.blogbus.com/logs/28117977.html vi/vim 中可以使用 :s 命令来替换字符串.该命令有很多种不同细节使用方法,可以实现复杂的功能,记 ...

  3. Form_如何通过标准功能查找数据源(概念)

    2014-06-01 Created By BaoXinjian

  4. 【转】一个对 Dijkstra 的采访视频

    一个对 Dijkstra 的采访视频 (也可以访问 YouTube 或者从源地址下载 MPEG1,300M) 之前在微博上推荐了一个对 Dijkstra 的采访视频,看了两遍之后觉得实在很好,所以再正 ...

  5. qsort函数、sort函数

    先说明一下qsort和sort,只能对连续内存的数据进行排序,像链表这样的结构是无法排序的. 首先说一下, qsort qsort(基本快速排序的方法,每次把数组分成两部分和中间的一个划分值,而对于有 ...

  6. struts2 ValueStack详解,页面获取值el表达式、ognl表达式

    http://www.blogjava.net/max/archive/2007/04/28/114417.html 我想用的是el表达式! http://www.cnblogs.com/belief ...

  7. PHP Client for Mysql Binlog

    PHP解析MySQL Binlog,依赖于mysql-replication-listener库 详见:https://github.com/bullsoft/php-binlog Install M ...

  8. app hybrid

    package com.note.testcases; /** * * The MIT License (MIT) * * Copyright (c) 2016 Alejandro Gómez Mor ...

  9. STORM在线业务实践-集群空闲CPU飙高问题排查(转)

    最近将公司的在线业务迁移到Storm集群上,上线后遇到低峰期CPU耗费严重的情况.在解决问题的过程中深入了解了storm的内部实现原理,并且解决了一个storm0.9-0.10版本一直存在的严重bug ...

  10. gcc自有的define语法,解决变量多次自加的问题

    如果定义一个这样的宏: #define MAX(a,b) ((a)>(b)?(a):(b)) int main(void){ int a=5,b=10; MAX(a++,b++); printf ...