.Net中log4net的三个配置实例

log4net配置方法一

1、下载log4net.dll,并添加到工程引用中。

2、在web.config中添加log4net的配置节点。打开web.config文件,找到节点<configuration>,在该节点下添加如下的lognet节点:
< configuration >
 < configSections >
  < section name = " log4net " type = " log4net.Config.Log4NetConfigurationSectionHandler,log4net-net-1.0 " />
 </ configSections >

 < log4net >
  < root >
   < level value = " ALL " />
   < appender - ref ref = " LogFileAppender " />
  </ root >
  < appender name = " LogFileAppender " type = " log4net.Appender.FileAppender " >
   < param name = " File " value = " log-file.txt " />
   < param name = " AppendToFile " value = " true " />
   < layout type = " log4net.Layout.PatternLayout " >
    < param name = " ConversionPattern " value = " %d [%t] %-5p %c [%x] &lt;%X{auth}&gt;%n - %m%n " />
   </ layout >
  </ appender >
 </ log4net >
</ configuration >

3、在应用程序代码中读取配置的三种方法

1> 该web项目中的AssemblyInfo.cs文件中添加log4net配置
[assembly:log4net.Config.DOMConfigurator( ConfigFile = " Web.config " ,Watch = true )]
如:
// 可以指定所有这些值,也可以使用“内部版本号”和“修订号”的默认值,
// 方法是按如下所示使用“*”:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion( " 1.0.0.0 " )]
[assembly: AssemblyFileVersion(
" 1.0.0.0 ")]
[assembly: log4net.Config.DOMConfigurator(ConfigFile = "Web.config", Watch = true)]

2> 在Global.asax.cs中添加log4net配置
[assembly:log4net.Config.DOMConfigurator( ConfigFile = " Web.config " ,Watch = true )]

3>在要记录日志的页面文件WebForm1.aspx.cs中添加log4net配置
[assembly:log4net.Config.DOMConfigurator( ConfigFile = " Web.config " ,Watch = true )]

也可以尝试在Global.asax中Application_Start里添加以下代码实现,
log4net.Config.XmlConfigurator.Configure( new System.IO.FileInfo( " Web.Config " ));

4、在页面WebForm的page_Load中初始化log4net实例,如在MyWebForm.aspx.cs的page_Load中添加以下代码:
log4net.ILog log = log4net.LogManager.GetLogger( " MyLogger " );

接下来就可以用log记录日志了,如:
log.Debug( " This is from log4net " );

log4net配置方法二——将log4net放到独立的文件中

1、在项目中添加log4net的引用

2、在web.config中添加log4net的配置节点。打开web.config文件,找到节点<configuration>,在该节点下添加如下的lognet节点:
< configSections >
< section name = " log4net " type = " log4net.Config.Log4NetConfigurationSectionHandler,log4net-net-1.0 " />
</ configSections >

3、新建一个文件log4net.config,文件内容如下:
<? xml version = " 1.0 " encoding = " utf-8 " ?>
< configuration >
< configSections >
< section name = " log4net "
type
= " log4net.Config.Log4NetConfigurationSectionHandler,log4net-net-1.0 "
/>
</ configSections >

< log4net >
< root >
< level value = " ALL " />
< appender - ref ref = " LogFileAppender " />
</ root >
< appender name = " LogFileAppender " type = " log4net.Appender.FileAppender " >
< param name = " File " value = " log-file.txt " />
< param name = " AppendToFile " value = " true " />
< layout type = " log4net.Layout.PatternLayout " >
< param name = " ConversionPattern " value = " %d [%t] %-5p %c [%x] &lt;%X{auth}&gt;%n - %m%n " />
</ layout >
</ appender >
</ log4net >
</ configuration >

4、在应用程序代码中读取配置的三种方法,方法同上,但里面的文件名 Web.Config要改成log4net.config,如在web项目中的AssemblyInfo.cs文件中添加log4net配置
[assembly: log4net.Config.DOMConfigurator(ConfigFile = " log4net.config " , Watch = true )]

5、web项目的WebForm1.aspx.cs Page_Load中实例化log4net并记录日志
log4net.ILog log = log4net.LogManager.GetLogger( " MyLogger " );
log.Debug(
" This is from log4net " );


log4net配置方法三

配置特点:
    1)log4net配置信息放在单独的文件中log4net.config中。
    2)把日志记录放在单独的一个公共项目中,这样配置文件只需要读取一次

1、新建日志记录项目LogProject

2、在LogProject中添加log4net.dll引用

3、在web项目的web.config中添加log4net的配置信息
< configSections >
< section name = " log4net " type = " log4net.Config.Log4NetConfigurationSectionHandler,log4net-net-1.0 " />
</ configSections >

4、建立单独的log4net.config配置文件
<? xml version = " 1.0 " encoding = " utf-8 " ?>
< configuration >
< configSections >
< section name = " log4net "
type
= " log4net.Config.Log4NetConfigurationSectionHandler,log4net-net-1.0 "
/>
</ configSections >

< log4net >
< root >
< level value = " ALL " />
< appender - ref ref = " LogFileAppender " />
</ root >
< appender name = " LogFileAppender " type = " log4net.Appender.FileAppender " >
< param name = " File " value = " log-file.txt " />
< param name = " AppendToFile " value = " true " />
< layout type = " log4net.Layout.PatternLayout " >
< param name = " ConversionPattern " value = " %d [%t] %-5p %c [%x] &lt;%X{auth}&gt;%n - %m%n " />
</ layout >
</ appender >
</ log4net >
</ configuration >

5、在LogProject项目中读取配置,即在LogProject的项目AssemblyInfo.cs文件中添加如下信息
[assembly:log4net.Config.DOMConfigurator( ConfigFile="log4net.config",Watch=true)]

6、在公共层的单独的写日志类中添加
public class Log4Nets{
  static ILog _log = log4net.LogManager.GetLogger( " MyLogger " );
  public static void Debug( string debugMsg)
  {
    _log.Debug(debugMsg);
  }
}

7、在web项目的webFom1.aspx.cs中Page_Load中直接饮用Debug,如
LogProject.Log4Nets.Debug( " This is from logenet " );

你可能感兴趣的:(C#,asp.net,log4net)