服务程序到生产系统无法跟踪错误,增加日志模块方便查错。使用log4net。引用两个概念:logger 和 appender。日志记录对象和日志记录的目标。
1.把log4net.dll加入引用
2.配置config
<
configSections
>
< section name ="log4net" type ="log4net.Config.Log4NetConfigurationSectionHandler, log4net" requirePermission ="false" />
</ configSections >
< section name ="log4net" type ="log4net.Config.Log4NetConfigurationSectionHandler, log4net" requirePermission ="false" />
</ configSections >
<
log4net
>
<!-- Define some output appenders -->
< appender name ="FileAppender" type ="log4net.Appender.FileAppender" >
< file value ="trace.log" />
< threshold value ="ALL" />
< encoding value ="utf-8" />
< appendToFile value ="true" />
< lockingModel type ="log4net.Appender.FileAppender+MinimalLock" />
< layout type ="log4net.Layout.PatternLayout" >
< conversionPattern value ="%date [%-5level] %logger - %message%newline" />
</ layout >
</ appender >
< root >
< priority value ="INFO" />
< appender-ref ref ="FileAppender" />
<!-- <appender-ref ref="ErrorAppender"/>
<appender-ref ref="ADONetAppender_SqlServer"/> -->
</ root >
</ log4net >
<!-- Define some output appenders -->
< appender name ="FileAppender" type ="log4net.Appender.FileAppender" >
< file value ="trace.log" />
< threshold value ="ALL" />
< encoding value ="utf-8" />
< appendToFile value ="true" />
< lockingModel type ="log4net.Appender.FileAppender+MinimalLock" />
< layout type ="log4net.Layout.PatternLayout" >
< conversionPattern value ="%date [%-5level] %logger - %message%newline" />
</ layout >
</ appender >
< root >
< priority value ="INFO" />
< appender-ref ref ="FileAppender" />
<!-- <appender-ref ref="ErrorAppender"/>
<appender-ref ref="ADONetAppender_SqlServer"/> -->
</ root >
</ log4net >
3.初始化配置
两种方式:
(1)assermbly里
[assembly: log4net.Config.XmlConfigurator()]
(2)类静态构造里
static
HRSADSResBroadcastSerivce()
{
XmlConfigurator.Configure();
}
{
XmlConfigurator.Configure();
}
4.定义全局变量
ILog logger
=
LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
5.just用它
try
{
DataProcess.HsvResInsert(resProcessed.reservationProcessKey, resProcessed.reservations[ 0 ].reservationKey);
}
catch (Exception e) // 订单插入失败,重新存回消息队列
{
Message msg = new Message();
msg.Formatter = queue.Formatter;
msg.Label = " hrs " ;
msg.Body = resProcessed;
queue.Send(msg);
logger.Error( " 插入数据库失败,消息重新存回队列 " , e);
continue ;
}
{
DataProcess.HsvResInsert(resProcessed.reservationProcessKey, resProcessed.reservations[ 0 ].reservationKey);
}
catch (Exception e) // 订单插入失败,重新存回消息队列
{
Message msg = new Message();
msg.Formatter = queue.Formatter;
msg.Label = " hrs " ;
msg.Body = resProcessed;
queue.Send(msg);
logger.Error( " 插入数据库失败,消息重新存回队列 " , e);
continue ;
}
PS:
(1) Layouts控制日志显示的格式样式。日志的显示格式如下:
"%timestamp [%thread] %-5level %logger - %message%newline"
Timestamp: 表示程序已经开始执行的时间。 单位[毫秒]。
Thread:执行当前代码的线程。
Level:日志的级别。
Logger:日志相关请求的名称。
Message: 日志消息。
(2) 日志的级别
Log4net支持多种级别的日志。优先级从高到低依次排列如下:
FATAL > ERROR > WARN > INFO > DEBUG
此外还有ALL(允许所有的日志请求)和OFF(拒绝所有的日志请求)这两种特殊的级别。
(3) 遗留一个问题:requirePermission="false" 做什么用的,谁知道赐教一下。