log4net(c#) 配置及使用

1. 首先从apache网站下载log4net, http://logging.apache.org/log4net/download_log4net.cgi 。我下的是最新版本 log4net-1.2.11-bin-newkey

2. 将 \bin\net\4.0\release\log4net.dll 复制到你的项目中 。

3. 将log4net.dll 添加引用到你的项目中。

4. 添加如下内容到 assemblyinfo.cs。

[assembly: log4net.Config.XmlConfigurator(ConfigFile="Log4Net.config", Watch=true)]  

注意: ConfigFile 可以指定相对路径 和 绝对路径。 eg: /log/xxxx.log  或者 d://log//xxxx.log

5.在项目中创建一个新的log4net的配置文件Log4Net.config。

  
  
    
    

注意:根据第4步的配置,应该把log4net的配置文件放到项目的bin/Debug 或者 bin/Release目录下,否则会出现找不到配置文件而无法创建logger对象。

(web的项目,直接放在web项目的根目录下即可)

 

6.在你的类中引入命名空间

using log4net;

7. 在你的类中创建logger 实例

private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);  

8. 运行你的项目,打出的log结果

[2012-06-26 14:14:34,862]  1 -- DEBUG -- LogTest.Program [Main] -- this is the log4net log test.  
[2012-06-26 14:14:34,877]  1 -- INFO  -- LogTest.Program [Main] -- this is the info..........................................  
[2012-06-26 14:14:34,878]  1 -- INFO  -- LogTest.Program [print] -- this method is print()  
[2012-06-26 14:14:34,878]  1 -- ERROR -- LogTest.Program [print] -- error test  

 

你可能感兴趣的:(C#,C#,log4net,日志)