文本日志

Posted on 2013-03-18 13:15  Roy_Ni  阅读(224)  评论(0编辑  收藏  举报

文本日志,顾名思义,就是将系统内部的信息、警告、错误、调试等信息记录在服务器上的系统文本文件上的一种归档日志。

文本日志的要求:

  1、可配置文本日志的记录位置;

  2、具备分文件写日志的能力;

  3、具备在系统的一处配置,其它各层都能调用的能力;

  4、能够记录程序产生日志的地点、时间、参与者和具体信息;

 

在实际应用中,发现有些比较好的第三方控件比较好用,能满足记录文本日志的要求。

本人用的比较多的是NLog组件。但因为格式和使用问题,对它做了一点小的封装。

 

TextLogger.cs

View Code
 1 public class TextLogger
 2     {
 3         private static readonly Logger _log = LogManager.GetCurrentClassLogger();
 4         private readonly string _logger;
 5 
 6         public TextLogger(string logger)
 7         {
 8             _logger = logger;
 9         }
10         /// <summary>
11         ///     记录正常信息日志
12         /// </summary>
13         public void Info(string format,params object[] args) { WriteLog(LogLevel.Info,null,format,args); }
14         /// <summary>
15         ///     记录错误信息日志
16         /// </summary>
17         public void Error(string format, params object[] args) { WriteLog(LogLevel.Error, null, format, args); }
18         /// <summary>
19         ///     记录错误信息日志
20         /// </summary>
21         public void Error(Exception ex,string format, params object[] args) { WriteLog(LogLevel.Error, ex, format, args); }
22         /// <summary>
23         ///     记录警告信息日志
24         /// </summary>
25         public void Warn(string format, params object[] args) { WriteLog(LogLevel.Warn, null, format, args); }
26         /// <summary>
27         ///     记录警告信息日志
28         /// </summary>
29         public void Warn(Exception ex, string format, params object[] args) { WriteLog(LogLevel.Warn, ex, format, args); }
30         /// <summary>
31         ///     记录跟踪日志
32         /// </summary>
33         /// <param name="format"></param>
34         /// <param name="args"></param>
35         public void Trace(string format, params object[] args) { WriteLog(LogLevel.Trace, null, format, args); }
36         /// <summary>
37         ///     记录跟踪日志
38         /// </summary>
39         public void Trace(Exception ex, string format, params object[] args) { WriteLog(LogLevel.Trace, ex, format, args); }
40 
41         private void WriteLog(LogLevel level,Exception ex,string format,params object[] args)
42         {
43             string message = string.Format(format ?? string.Empty, args);
44             if (ex != null) message = string.Concat(message, "\r\n", ex.Message);
45             message = string.Concat(_logger, "\r\n", message);
46             message = string.Concat(message, "\r\n--------------------------------------------------------\r\n");
47             _log.Log(level,message);
48         }
49     }

 

TextLogManager.cs

View Code
 1 public static class TextLogManager
 2     {
 3         /// <summary>
 4         /// 创建日志记录器
 5         /// </summary>
 6         /// <param name="type">所在类类名</param>
 7         /// <returns>返回文本日志记录器</returns>
 8         public static TextLogger Create(Type type)
 9         {
10             return Create(type.FullName);
11         }
12 
13         /// <summary>
14         /// 创建日志记录器
15         /// </summary>
16         /// <param name="logger">表述日志发生位置</param>
17         /// <returns>返回文本日志记录器</returns>
18         public static TextLogger Create(string logger)
19         {
20             return new TextLogger(logger);
21         }
22     }

 

调用方法:

View Code
1 private readonly TextLogger _tracing = TextLogManager.Create(typeof(FinancialDao));
2 
3 
4 _tracing.Error(“”);

 

配置文件:

View Code
 1 <configuration>
 2   <configSections>
 3     <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
 4 <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 5     <targets>
 6       <target name="file" xsi:type="File" fileName="D:/Logs/${date:format=yyyyMM}/log_${date:format=ddHH}.log" layout="[${date:format=yyyy-MM-dd HH\:mm\:ss}][${level}] ${message} ${exception}"/>
 7     </targets>
 8     <rules>
 9       <logger name="*" minlevel="Debug" writeTo="file"/>
10     </rules>
11   </nlog>
12  </configSections>
13 </configuration>