NLog详细教程-2022/12/01

NLog详细教程

最近公司使用到了NLog来进行一个日志记录功能,了解了一下,觉得很实用,特此做个分享。

小时候看迪迦,里面一段话特别印象深刻,“努力活完短暂的一生,将成果留给后代继承,人类就是如此反复,真的很了不起!”这些开源软件就是最好的证明,前人帮我们造好了一个又一个轮子,我们需要好好的使用,为往圣继绝学。

NLog的具体介绍,功能,优点等网上一大推,就不具体介绍了,主要说怎么安装使用

安装

需要安装NuGet包。

1.在需要使用的项目点击鼠标右键
在这里插入图片描述
2.点击浏览->输入名字->选择如图所示的程序包(这恐怖的下载量)->点击安装(教程写的日期是2022/12/01,NLog最新版就是5.1.0)
在这里插入图片描述3.点击确定
在这里插入图片描述
4.在项目的引用中可以看到NLog这个dll就算安装成功了。
在这里插入图片描述

初级使用

配置文件编写
NLog配置原理

NLog将自动在某些默认位置中搜索其配置文件。当NLog和标准的exe文件配合使用时,将自动按照顺序搜索下列路径,以得到配置文件:

1.应用程序的标准配置文件(通常为applicationname.exe.config)
2.应用程序所在目录中的applicationname.exe.nlog文件
3.应用程序所在目录中的NLog.config文件
4.NLog.dll所在目录中的NLog.dll.nlog文件
5.环境变量NLOG_GLOBAL_CONFIG_FILE所指向的文件
NLog配置文件形式

1.可以写在项目中的App.config文件中。使用标准化的configSection机制
在这里插入图片描述
2.可以单独写一个配置文件负责日志记录(取名NLog)
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
推荐还是单独写一个配置文件,毕竟健壮性更好。

最精简的配置文件
<?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" >
  <targets>
    <!--输出目标:name名称f,xsi:type输出类型文件, fileName输出到程序根目录logs文件夹中, 以日期作为生成log文件名称, layout生成内容的格式-->
    <target name="f"
            xsi:type="File"
            fileName="${basedir}/logs/${shortdate}.log"
            layout="${longdate} ${uppercase:${level}} ${message}" />
  </targets>
  <rules>
      <!--日志路由规则:最低级别Debug,输出到target目标f-->
    <logger name="*" minlevel="Debug" writeTo="f" />
  </rules>
</nlog>

要记得将NLog的文件属性设置为始终复制。
在这里插入图片描述

代码实现
using NLog;
using NLog.Config;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
   

    public partial class Form1 : Form
    {
   
        //创建日志记录对象
        private Logger Logger = NLog.LogManager.GetCurrentClassLogger();
        public Form1()
        {
   
            InitializeComponent();
            //初始化配置日志
            LogManager.Configuration 
  • 13
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
<?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="false" internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log"> <!-- optional, add some variables https://github.com/nlog/NLog/wiki/Configuration-file#variables --> <variable name="myvar" value="myvalue"/> <!-- See https://github.com/nlog/nlog/wiki/Configuration-file for information on customizing logging rules and outputs. --> <targets> <!-- add your targets here See https://github.com/nlog/NLog/wiki/Targets for possible targets. See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers. --> <!-- Write events to a file with the date in the filename. <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log" layout="${longdate} ${uppercase:${level}} ${message}" /> --> <target xsi:type="File" name="f" fileName="logs/${shortdate}.log" archiveFileName="logs/archive.{#}.log" archiveEvery="Day" archiveNumbering="Rolling" maxArchiveFiles="30" concurrentWrites="true" keepFileOpen="false" encoding="utf-8"> <layout xsi:type="CsvLayout" delimiter="auto" withHeader="true" > <column name="time" layout="${longdate:universalTime=false}" /> <column name="threadid" layout="${threadid}"/> <column name="level" layout="${level:upperCase=true}"/> <column name="callsite" layout="${callsite:includeSourcePath=true}" /> <column name="message" layout="${message}" /> <column name="stacktrace" layout="${callsite:includeSourcePath=true}" /> <column name="exception" layout="${exception:format=ToString}"/> </layout> </target> <target xsi:type="File" name="c" layout="${threadid} ${uppercase:${level}} ${message} ${callsite} ${exception:format=tostring}" /> </targets> <rules> <!-- add your logging rules here --> <!-- Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace) to "f" <logger name="*" minlevel="Debug" writeTo="f" /> --> <logger name="*" writeTo="f,c" /> </rules> </nlog>NLog这个配置给我怎么加注释说明
06-04
NLog配置文件中添加注释可以使用XML的注释标记`<!-- 注释内容 -->`,例如: ``` <?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="false" internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log"> <!-- 定义变量 --> <variable name="myvar" value="myvalue"/> <!-- 定义日志输出目标 --> <targets> <!-- 文件日志 --> <target xsi:type="File" name="f" fileName="logs/${shortdate}.log" archiveFileName="logs/archive.{#}.log" archiveEvery="Day" archiveNumbering="Rolling" maxArchiveFiles="30" concurrentWrites="true" keepFileOpen="false" encoding="utf-8"> <layout xsi:type="CsvLayout" delimiter="auto" withHeader="true" > <column name="time" layout="${longdate:universalTime=false}" /> <column name="threadid" layout="${threadid}"/> <column name="level" layout="${level:upperCase=true}"/> <column name="callsite" layout="${callsite:includeSourcePath=true}" /> <column name="message" layout="${message}" /> <column name="stacktrace" layout="${callsite:includeSourcePath=true}" /> <column name="exception" layout="${exception:format=ToString}"/> </layout> </target> <!-- 控制台日志 --> <target xsi:type="File" name="c" layout="${threadid} ${uppercase:${level}} ${message} ${callsite} ${exception:format=tostring}" /> </targets> <!-- 定义日志输出规则 --> <rules> <logger name="*" writeTo="f,c" /> </rules> </nlog> ``` 在需要注释的地方添加注释即可,例如: ``` <!-- 定义变量 --> <variable name="myvar" value="myvalue"/> <!-- 定义日志输出目标 --> <targets> <!-- 文件日志 --> <target xsi:type="File" name="f" fileName="logs/${shortdate}.log" archiveFileName="logs/archive.{#}.log" archiveEvery="Day" archiveNumbering="Rolling" maxArchiveFiles="30" concurrentWrites="true" keepFileOpen="false" encoding="utf-8"> <layout xsi:type="CsvLayout" delimiter="auto" withHeader="true" > <column name="time" layout="${longdate:universalTime=false}" /> <column name="threadid" layout="${threadid}"/> <column name="level" layout="${level:upperCase=true}"/> <column name="callsite" layout="${callsite:includeSourcePath=true}" /> <column name="message" layout="${message}" /> <column name="stacktrace" layout="${callsite:includeSourcePath=true}" /> <column name="exception" layout="${exception:format=ToString}"/> </layout> </target> <!-- 控制台日志 --> <target xsi:type="File" name="c" layout="${threadid} ${uppercase:${level}} ${message} ${callsite} ${exception:format=tostring}" /> </targets> <!-- 定义日志输出规则 --> <rules> <logger name="*" writeTo="f,c" /> </rules> ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值