.Net日志之NLog

       大家写日志类肯定会遇到这样一个问题:文件xxx正在被另一个进程使用,这是我们手写日志类的通病,因为要记录日志的地方实在太多了,所以难免会出现两个进程同时访问日志操作类的情况,这个时候就需要用到别人封装好的日志类来解决问题。当然大牛的话自己重新封装一个也可以,但大多数人还是用别人写好的直接拿来用比较实际。

       我第一次用是用的log4net,但是log4net的配置文件搞了很久都没明白,始终不能输出日志,如果有同样困扰的童鞋请往下看:

然后我的老师推荐我使用NLog,NLog的配置文件简直不要太简单,一次点亮!不对不对,一次就输出日志成功!

介绍一下我使用NLog的步骤

  1. 首先VS->工具->NuGet包管理器->程序包管理器控制台->输入 Install- Package NLog
  2. (上面一步会直接下载安装完成)完成后,我们需要一个配置文件NLog.config,右键点击项目->添加新项目->选择web配置文件
  3. <?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">
    
    
      <targets>
        <!--保存至文件-->
        <!--filename用来定义输入日志的文件位置和文件名-->
        <!--layout用来定义输入日志的格式-->
        <target name="error_file" xsi:type="File" maxArchiveFiles="30"
                fileName="${basedir}/Logs/Error/${shortdate}.txt"
                layout="${longdate} | ${level:uppercase=false:padding=-5} | ${message} ${onexception:${exception:format=tostring} ${newline} ${stacktrace} ${newline}" />
        <target name="debugger_file" xsi:type="File" maxArchiveFiles="30"
                fileName="${basedir}/Logs/SQL/${shortdate}.txt"
                layout="${longdate} | ${level:uppercase=false:padding=-5} | ${message} ${onexception:${exception:format=tostring} ${newline} ${stacktrace} ${newline}" />
        <target name="info_file" xsi:type="File" maxArchiveFiles="30"
                fileName="${basedir}/Logs/Info/${shortdate}.txt"
                layout="${longdate} | ${level:uppercase=false:padding=-5} | ${message} ${onexception:${exception:format=tostring} ${newline} ${stacktrace} ${newline}" />
    
      </targets>
      <rules>
        <logger name="*" minlevel="Debug" writeTo="debugger_file" />
        <logger name="*" minlevel="Error" writeTo="error_file" />
        <logger name="*" minlevel="Info" writeTo="info_file" />
      </rules>
    </nlog>
    

    一般来说复制一下涂涂改改就好了

  4. 定义一个日志帮助类,封装成一个公共类最好,系统各个部分都可以调用

    public class LogHelper
    {
        NLog.Logger logger;
        public LogHelper(NLog.Logger logger)
        {
            this.logger = logger;
        }
    
        public LogHelper(string name) : this(NLog.LogManager.GetLogger(name))
        {
        }
    
        public void Debug(string msg, params object[] args)
        {
            logger.Debug(msg, args);
        }
    
        public void Debug(string msg, Exception err, params object[] args)
        {
            logger.Debug(msg, err, args);
        }
    
        public void Info(string msg, params object[] args)
        {
            logger.Info(msg, args);
        }
    
        public void Info(string msg, Exception err, params object[] args)
        {
            logger.Info(msg, err, args);
        }
    
        public void Trace(string msg, params object[] args)
        {
            logger.Trace(msg, args);
        }
    
        public void Trace(string msg, Exception err, params object[] args)
        {
            logger.Trace(msg, err, args);
        }
    
        public void Error(string msg, params object[] args)
        {
            logger.Error(msg, args);
        }
    
        public void Error(string msg, Exception err, params object[] args)
        {
            logger.Error(msg, err, args);
        }
    
        public void Fatal(string msg, params object[] args)
        {
            logger.Fatal(msg, args);
        }
    
        public void Fatal(string msg, Exception err, params object[] args)
        {
            logger.Fatal(msg, err, args);
        }

    我之前复制的别人的有些地方报错,我自己做了修改。其中logger.Error(xx,xx)提示已过时,给他新增一个param object[] arg的参数就好了

  5. 最后就是直接在需要输入日志的地方实例化一个

     public static LogHelper log = new LogHelper("NLog.config");

    再直接

    log.Error(ex.Message);
    log.Info(ex.Message);
    log.Debug(ex.Message);
    log.Fatal(ex.Message);
    log.Trace(ex.Message);

    输入日志就好了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值