代码改变世界

ASP.Net2.0使用Log4Net(二)

2007-11-24 17:39  敏捷的水  阅读(2221)  评论(5编辑  收藏  举报
(二)使用单独的配置文件,不放在web.config里
 1.新建一website.
 2.添加一个类库工程,取名LogNet
 
3. 为LogNet工程引用log4net.dll
4. 双击打开AssemblyInfo.cs在最后添加
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
5. 为LogNet工程添加一个logBLL.cs文件,我们把日志调用封装到这一层


using System;
using System.Collections.Generic;
using System.Text;
namespace LogNet
{
    
public class LogBLL
    
{
        
public static void debug(string message)
        
{
            log4net.ILog log 
= log4net.LogManager.GetLogger("Test");
            
if (log.IsDebugEnabled)
            
{
                log.Debug(message);
            }

            log 
= null;
        }

        
public static void error(string message)
        
{
            log4net.ILog log 
= log4net.LogManager.GetLogger("Test");
            
if (log.IsErrorEnabled)
            
{
                log.Error(message);
            }

            log 
= null;
        }

        
public static void fatal(string message)
        
{

            log4net.ILog log 
= log4net.LogManager.GetLogger("Test");
            
if (log.IsFatalEnabled)
            
{
                log.Fatal(message);
            }

            log 
= null;
        }

        
public static void info(string message)
        
{
            log4net.ILog log 
= log4net.LogManager.GetLogger("Test");
            
if (log.IsInfoEnabled)
            
{
                log.Info(message);
            }

            log 
= null;
        }


        
public static void warn(string message)
        
{
            log4net.ILog log 
= log4net.LogManager.GetLogger("Test");
            
if (log.IsWarnEnabled)
            
{
                log.Warn(message);
            }

            log 
= null;
        }

    }

}



6.在web站点根目录下添加一个log4net.config文件,内容如下
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    
<configSections>
        
<section name="log4net"  type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
    
</configSections>
    
<log4net>
        
<root>
            
<!--
            <level value="ALL" />
            <appender-ref ref="rootFile" />
            
-->
        
</root>
        
<logger name="Test">
            
<level value="DEBUG" />
            
<appender-ref ref="rollingFile" />
        
</logger>
        
<appender  name="rollingFile" type="log4net.Appender.RollingFileAppender,log4net" >
            
<param name="File" type=""  value="log/" />
            
<param name="AppendToFile" value="true" />
            
<param name="RollingStyle" value="Date" />
            
<param name="DatePattern" value="yyyyMMdd" />
            
<param name="StaticLogFileName" value="false" />
            
<layout type="log4net.Layout.PatternLayout,log4net">               
                
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
                
<param name="Header" value=" ----------------------header-------------- " />
                
<param name="Footer" value=" ----------------------footer-------------- " />        
            
</layout>
        
</appender>        
    
</log4net>
</configuration>
7.编译LogNet工程
8.为web引入LogNet工程bin目录下的LogNet.dll
9.在default.aspx.cs里写入如下代码
using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using LogNet; //引用程序集
public partial class _Default : System.Web.UI.Page 
{
    
protected void Page_Load(object sender, EventArgs e)
    {
        
if (!Page.IsPostBack)
        {
            LogBLL.debug(
"测试信息");          
        }
    }
}

10.运行,将在web目录下的log目录下看到日志文件
  ----------------------header--------------------------
2007-11-24 12:16:24,171 [4] DEBUG LiveBookings - 测试信息
2007-11-24 12:16:46,312 [4] DEBUG LiveBookings - 测试信息
----------------------footer--------------------------
本文源码下载: 点击这里下载