如何给你的web项目加入log4net支持
1) 下载到log4net的dll文件: log4net.dll.
下载好后,你可以将气拷贝到web项目的bin目录,然后给你的web项目加入该文件的reference.
2) 配置log4net.config文件, 代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
<?xml version="1.0" encoding="utf-8" ?>
<log4net debug="false">
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="Log/MyBlog.log"/>
<param name="datePattern" value="YY-MM-dd HH:mm"/>
<param name="AppendToFile" value="true"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n"/>
</layout>
</appender>
<appender name="HttpTraceAppender" type="log4net.Appender.ASPNetTraceAppender">
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n"/>
</layout>
</appender>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n"/>
</layout>
</appender>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="log\MyBlog"/>
<appendToFile value="true"/>
<rollingStyle value="Date"/>
<datePattern value="_yyyyMMdd'.log'"/>
<staticLogFileName value="false"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t] %-5p %c - %m%n"/>
</layout>
</appender>
<root>
<level value="INFO"/>
<!--<level value="ERROR" />-->
<appender-ref ref="RollingLogFileAppender"/>
</root>
</log4net>
<log4net debug="false">
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="Log/MyBlog.log"/>
<param name="datePattern" value="YY-MM-dd HH:mm"/>
<param name="AppendToFile" value="true"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n"/>
</layout>
</appender>
<appender name="HttpTraceAppender" type="log4net.Appender.ASPNetTraceAppender">
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n"/>
</layout>
</appender>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n"/>
</layout>
</appender>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="log\MyBlog"/>
<appendToFile value="true"/>
<rollingStyle value="Date"/>
<datePattern value="_yyyyMMdd'.log'"/>
<staticLogFileName value="false"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t] %-5p %c - %m%n"/>
</layout>
</appender>
<root>
<level value="INFO"/>
<!--<level value="ERROR" />-->
<appender-ref ref="RollingLogFileAppender"/>
</root>
</log4net>
3) 配置Web.Config文件,代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
...
<log4net configSource="log4net.config"/>
<system.web>
</configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
...
<log4net configSource="log4net.config"/>
<system.web>
</configuration>
4) 在Global.asax文件中加入如下代码:
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
using log4net.Config;
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
XmlConfigurator.Configure();
}
...
}
{
protected void Application_Start(object sender, EventArgs e)
{
XmlConfigurator.Configure();
}
...
}
5) 在客户端类中按如下方法加入日志:
using log4net;
// 将logger作为类的成员变量
private static readonly ILog logger = LogManager.GetLogger(typeof(类名));
...
logger.Info("信息日志");
logger.Error("错误日志");
private static readonly ILog logger = LogManager.GetLogger(typeof(类名));
...
logger.Info("信息日志");
logger.Error("错误日志");
技术改变世界