如何在VS2010下使用Log4Net

log4net has a known issue of referencing a System.Web component which is not part of the .NET Framework 4 Client Profile and the VS2010 by default sets project target to be the lightweighted Client Profile. The solution is to change the target to .NET Framework 4:

Right click project -> properties -> Application. Change:

 
.
NET 
Framework

 
4

 
Client

 
Profile

 


to be:

 
.
NET 
Framework

 
4

 


... and that should work, i.e. you can use log4net again...

来自:http://stackoverflow.com/questions/1767253/log4net-with-net-4-0

 范例:

App.config

 

  1. <? xml   version = "1.0" ?>   
  2. < configuration >   
  3.   < configSections >   
  4.     < section   name = "log4net"   type ="log4net.Config.Log4NetConfigurationSectionHandler,   
  5.  log4net-net-1.0" />   
  6.   </ configSections >   
  7.   < log4net >   
  8.     < root >   
  9.       < level   value = "ALL"   />   
  10.       < appender-ref   ref = "LogFileAppender"   />   
  11.     </ root >   
  12.     < appender   name = "LogFileAppender"   type = "log4net.Appender.FileAppender"   >   
  13.       < param   name = "File"   value = "log-file.txt"   />   
  14.       < param   name = "AppendToFile"   value = "true"   />   
  15.       < layout   type = "log4net.Layout.PatternLayout" >   
  16.         < param   name = "ConversionPattern"   value = "%d [%t] %-5p %c [%x] <%X{auth}>%n - %m%n"   />   
  17.       </ layout >   
  18.     </ appender >   
  19.   </ log4net >   
  20.   < startup >   
  21.     < supportedRuntime   version = "v4.0"   sku = ".NETFramework,Version=v4.0" />   
  22.   </ startup >   
  23. </ configuration >   

注意:configSections节点和startup节点的顺序,否则只见文件而无法输出日志

AssemblyInfo.cs中添加

  1. [assembly: log4net.Config.DOMConfigurator(ConfigFileExtension =  "config" , Watch =  true )]  

.cs代码正常使用

  1. log4net.ILog log = log4net.LogManager.GetLogger( "MyLogger" );   
  2. log.Debug("test" );  
  3. 原文地址:http://blog.csdn.net/Maths_bai/archive/2010/12/13/6074337.aspx
posted @ 2011-06-17 11:19  qb371  阅读(249)  评论(0编辑  收藏  举报