(二)使用单独的配置文件,不放在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文件,内容如下
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--------------------------