NLog Helpper日志帮助类配置和使用

1.帮助类  (首先需要引入NLog.dll)

  1 using System;
  2 
  3 namespace Hwapu.CTB.Model
  4 {
  5     /// <summary>
  6     /// 用法实例 : NLogTest.NlogInstance log = new NLogTest.NlogInstance("NameSpace.ClassName.FunctionName");
  7     /// log.Debug();log.Error();
  8     /// </summary>
  9     public class NlogInstance : IDisposable
 10     {
 11         private bool alreadyDisposed = false;
 12         private NLog.Logger logger;
 13 
 14         private NlogInstance(NLog.Logger logger)
 15         {
 16             this.logger = logger;
 17         }
 18 
 19         public NlogInstance(string name)
 20             : this(NLog.LogManager.GetLogger(name))
 21         {
 22         }
 23 
 24         public static NlogInstance Default { get; private set; }
 25 
 26         static NlogInstance()
 27         {
 28             Default = new NlogInstance(NLog.LogManager.GetCurrentClassLogger());
 29         }
 30 
 31         public void Debug(string msg, params object[] args)
 32         {
 33             logger.Debug(msg, args);
 34         }
 35 
 36         public void Debug(string msg, Exception err)
 37         {
 38             logger.Debug(msg, err);
 39         }
 40 
 41         public void Info(string msg, params object[] args)
 42         {
 43             logger.Info(msg, args);
 44         }
 45 
 46         public void Info(string msg, Exception err)
 47         {
 48             logger.Info(msg, err);
 49         }
 50 
 51         public void Trace(string msg, params object[] args)
 52         {
 53             logger.Trace(msg, args);
 54         }
 55 
 56         public void Trace(string msg, Exception err)
 57         {
 58             logger.Trace(msg, err);
 59         }
 60 
 61         public void Error(string msg, params object[] args)
 62         {
 63             logger.Error(msg, args);
 64         }
 65 
 66         public void Error(string msg, Exception err)
 67         {
 68             logger.Error(msg, err);
 69         }
 70 
 71         public void Fatal(string msg, params object[] args)
 72         {
 73             logger.Fatal(msg, args);
 74         }
 75 
 76         public void Fatal(string msg, Exception err)
 77         {
 78             logger.Fatal(msg, err);
 79         }
 80 
 81         protected void Dispose(bool disposing)
 82         {
 83             if (alreadyDisposed) return; //保证不重复释放
 84             if (disposing)
 85             {
 86                 logger = null;
 87             }
 88             alreadyDisposed = true;
 89         }
 90 
 91         public void Dispose()
 92         {
 93             //调用带参数的Dispose方法,释放托管和非托管资源
 94             Dispose(true);
 95             //手动调用了Dispose释放资源,那么析构函数就是不必要的了,这里阻止GC调用析构函数
 96             System.GC.SuppressFinalize(this);
 97         }
 98 
 99         ~NlogInstance()
100         {
101             Dispose(false);
102         }
103     }
104 
105 }

2.配置  Nlog.config

 1 <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 2   <targets>
 3     <target name="Trace" xsi:type="File"
 4                     fileName="${basedir}/Logs/${shortdate}/Trace.txt" maxArchiveFiles="30"
 5                     layout="${longdate} | ${level:uppercase=false} | ${message} ${onexception:${exception:format=tostring} ${newline} ${stacktrace} ${newline}" />
 6     <target name="Trace_Error" xsi:type="File"     
 7   fileName="${basedir}/Logs/${shortdate}/Trace_Error.txt" maxArchiveFiles="30"
 8                     layout="${longdate} | ${level:uppercase=false} | ${message} ${onexception:${exception:format=tostring} ${newline} ${stacktrace} ${newline}" />
 9   </targets>
10   <rules>
11     <logger name="*" level="Info" writeTo="Trace" />
12     <logger name="*" level="Error" writeTo="Trace_Error" />
13   </rules>
14 </nlog>

 

上一篇:c# – Web.config转换移动命名空间声明


下一篇:c# – 如何避免NLog.Config中的冗余?