Fork me on GitHub

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>

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

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

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

总结

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

posted @ 2018-03-07 17:37  大葱哥  阅读(1215)  评论(0编辑  收藏  举报