在.net开发中使用Log4Net组件


log4net是apache组织开发的日志组件, 同其姐妹log4j一样, 是一个开源项目. 可以以插件的形式应用在你的系统中. 下面仅说明如何应用在web forms项目中. 做为主要的日志输出组件.

1. 首先你应该下载log4net.dll并引入到你的项目References中.
2. 需要修改你的global.asa.cs. 配置application对象启动的时候加载log4net配置. 这一步是不可以缺少的.

protected   void  Application_Start(Object sender, EventArgs e)
... {
   
//若是直接在Web.config里面则:
   log4net.Config.DOMConfigurator.Configure();
   
//若在单独的文件里,则:
   log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(Server.MapPath("~/")+"\log4net.config"));

}


3. 可以看到上面的代码没有参数. 可见是载入了缺省配置. 该配置必须设置于网站根目录下的log4net.config文件中.
具体内容如下:

<? xml version = " 1.0 "  encoding = " utf-8 "   ?>
< configuration >
  
< configSections >
  
< section name = " log4net "  type = " log4net.Config.Log4NetConfigurationSectionHandler, log4net "   />
  
</ configSections >
< log4net debug = " false " >
  
< appender name = " LogFileAppender "  type = " log4net.Appender.FileAppender "   >
    
< param name = " File "  value = " XxxxApplication.log.txt "   />
    
< param name = " datePattern "  value = " MM-dd HH:mm "   />
    
< param name = " AppendToFile "  value = " true "   />
    
< layout type = " log4net.Layout.PatternLayout " >
      
< param name = " ConversionPattern "  value = " %d [%t] %-5p %c [%x] - %m%n "   />
    
</ layout >
  
</ appender >
  
< appender name = " HttpTraceAppender "  type = " log4net.Appender.ASPNetTraceAppender "   >
    
< layout type = " log4net.Layout.PatternLayout " >
      
< param name = " ConversionPattern "  value = " %d [%t] %-5p %c [%x] - %m%n "   />
    
</ layout >
  
</ appender >
  
< appender name = " EventLogAppender "  type = " log4net.Appender.EventLogAppender "   >
    
< layout type = " log4net.Layout.PatternLayout " >
      
< param name = " ConversionPattern "  value = " %d [%t] %-5p %c [%x] - %m%n "   />
    
</ layout >
  
</ appender >
  
< appender name = " RollingLogFileAppender "  type = " log4net.Appender.RollingFileAppender " >
    
< param name = " File "  value = " _LogDataLog.txt "   />
    
< param name = " AppendToFile "  value = " true "   />
    
< param name = " MaxSizeRollBackups "  value = " 10 "   />
    
< param name = " MaximumFileSize "  value = " 5MB "   />
    
< param name = " RollingStyle "  value = " Size "   />
    
< param name = " StaticLogFileName "  value = " true "   />
    
< layout type = " log4net.Layout.PatternLayout " >
      
< param name = " ConversionPattern "  value = " %d [%t] %-5p %c [%x] - %m%n "   />
    
</ layout >
  
</ appender >
  
< root >
    
< level value = " DEBUG "   />
    
< appender - ref   ref = " LogFileAppender "   />
  
</ root >
</ log4net >
</ configuration >


5. 以上定义了多个appender. 简单来说, 每一个 appender 都是一种输出介质.
6. root节点指定了选用的 appender. 本例选用了LogFileAppender. (文本文件输出). 在Appender定义中定义了输出的格式. 和目标文本文件所在位置. (起始位置是应用程序根目录. (web.config所在目录).
7. 到目前位置就配置好了log4net. 可以在我们的应用中直接使用了.
8. 以下说明应用方法:
要输出日志, 必须首先得到带有一个别名的logger.
使用以下命令
(C#):

using  log4net;
using  log4net.Config;

public   partial   class  test_log : System.Web.UI.Page
... {
    
private static readonly ILog log = LogManager.GetLogger("tets_log");
   
    
protected void Page_Load(object sender, EventArgs e)
    
...{
        log.Info(
"Page_Load");
    }

    
protected void Button1_Click(object sender, EventArgs e)
    
...{
        log.Info(
"Hello World, I am a logger");
    }

}


log4net.ILog Logger logger = log4net.LogManager.GetLogger(this.GetType());
(可以直接使用GetType得到当前类名)
之后调用
logger.Info(string message);
logger.Error(string message);
logger.Debug(string message);
即可输出日志.

调试后可查找应用程序根目录下是否已经自动创建XxxxxApplication.log.txt文本文件.以及是否正确输出了日志.

log4net是一个非常完善的日志组件. 有着强大的可配置性. 有助于提高开发效率.

log4net.config文件如下:
<? xml version = " 1.0 "  encoding = " utf-8 "   ?>
< configuration >
  
< configSections >
  
< section name = " log4net "  type = " log4net.Config.Log4NetConfigurationSectionHandler, log4net "   />
  
</ configSections >
< log4net debug = " false " >
  
< appender name = " LogFileAppender "  type = " log4net.Appender.FileAppender "   >
    
< param name = " File "  value = " XxxxApplication.log.txt "   />
    
< param name = " datePattern "  value = " MM-dd HH:mm "   />
    
< param name = " AppendToFile "  value = " true "   />
    
< layout type = " log4net.Layout.PatternLayout " >
      
< param name = " ConversionPattern "  value = " %d [%t] %-5p %c [%x] - %m%n "   />
    
</ layout >
  
</ appender >
  
< appender name = " HttpTraceAppender "  type = " log4net.Appender.ASPNetTraceAppender "   >
    
< layout type = " log4net.Layout.PatternLayout " >
      
< param name = " ConversionPattern "  value = " %d [%t] %-5p %c [%x] - %m%n "   />
    
</ layout >
  
</ appender >
  
< appender name = " EventLogAppender "  type = " log4net.Appender.EventLogAppender "   >
    
< layout type = " log4net.Layout.PatternLayout " >
      
< param name = " ConversionPattern "  value = " %d [%t] %-5p %c [%x] - %m%n "   />
    
</ layout >
  
</ appender >
  
< appender name = " RollingLogFileAppender "  type = " log4net.Appender.RollingFileAppender " >
    
< param name = " File "  value = " _LogDataLog.txt "   />
    
< param name = " AppendToFile "  value = " true "   />
    
< param name = " MaxSizeRollBackups "  value = " 10 "   />
    
< param name = " MaximumFileSize "  value = " 5MB "   />
    
< param name = " RollingStyle "  value = " Size "   />
    
< param name = " StaticLogFileName "  value = " true "   />
    
< layout type = " log4net.Layout.PatternLayout " >
      
< param name = " ConversionPattern "  value = " %d [%t] %-5p %c [%x] - %m%n "   />
    
</ layout >
  
</ appender >
  
< root >
    
< level value = " DEBUG "   />
    
< appender - ref   ref = " LogFileAppender "   />
  
</ root >
</ log4net >
</ configuration >  

你可能感兴趣的:(C++,c,Web,.net,C#)