正文:(二)使用单独的配置文件,不放在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" />