NLog 配置与使用

news/2024/7/4 9:26:44

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

NuGet 添加组件

jBC93U3.png

配置 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. 错误邮件提示

总结

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

转载于:https://www.cnblogs.com/dacongge/p/8523833.html


http://www.niftyadmin.cn/n/1998735.html

相关文章

打造CSDN里最有个性的Blog版面(HTML+CSS)

呵呵&#xff0c;从昨天到今天一直在弄CSDN的Blog美化。本来我最讨厌的就是网页编程了&#xff0c;不过没办法&#xff0c;自己的Blog不好看。。。。所以HTML和CSS从0开始自学了满满24小时&#xff0c;做了现在这个版面。。。。 说说我这24小时的一点点经验吧。开始的时候我是打…

php代码编写注册登录页面,PHP开发登录注册完整代码之注册HTML页面

创建 reg.html 文件我们此页面有一个表单&#xff0c;里面有四个input输入框&#xff0c;我们用JS对输入框里面的内容作了登录判断&#xff0c;并使用了css对表单进行了布局。代码如下html>登陆界面function checkinput(){if(myform.name.value""){alert("请…

通过Live Writer Blog客户端来写blog

通过Live Writer Blog客户端来写blog 前阵子微软件发布了一款管理个人blog的客户端工具&#xff1a;Windows Live Writer&#xff08;Beta版&#xff09;。它不光能够支持Windows Live Spaces的用户使用&#xff0c;同时它也支持现在网络上其它流行的一系列Blog内容管理系统。前…

php招聘需要笔试吗,PHP招聘笔试部分

嗯&#xff0c;基本上这些题都答得好那就…直接上题.1. 基本知识点1. HTTP协议中几个状态码的含义:503 500 401 200 301 302。。。2. Include require include_once require_once 的区别.3. PHP/Mysql中几个版本的进化史&#xff0c;比如mysql4.0到4.1&#xff0c;PHP 4.x到5.1…

★ 看看你的博客值多少钱 ★

★ 看看你的博客值多少钱 ★ 刚一个朋友发过一个网站&#xff0c;是说有个叫的提供了一个博客价值评估工具。该工具能通过输入你的博客地址之后&#xff0c;由它根据Alexa排名、各大搜索引擎搜索记录数、RSS订阅数进行综合评估&#xff0c;完后给出相应参考价值。进入博易网htt…

test for my first artical by client software

Hey, come on! I get something to tell you! Something to put my heart! If you love me. Please stay! Dont go away! andylin

php使用phpmailer,php 发邮件(使用phpmailer类)

利用php中的phpmailer发送邮件的示例&#xff0c;示例代码以及该示例的页面效果如下&#xff0c;另外文章的末尾提供了phpmailer文件的下载地址&#xff0c;该文件里面有许多发送邮件的例子&#xff0c;不过都是英文的。具体代码&#xff1a;利用phpmailer发送电子邮件if(isset…

关于vue的懒加载实践

最近在研究vue的按需加载&#xff0c;好奇怪&#xff0c;之前好像并没有看到vue的官文里面有这一部分&#xff0c;是我看差了吗hahaha~尬笑~ 其实只需要看vue-router官文就可以了&#xff0c;里面有懒加载的讲解&#xff0c;并且附带了详细内容的连接。一个一个看过去&#xff…