1)创建Global.asax
protected void Application_Start(object sender, EventArgs e)
{
log4net.Config.XmlConfigurator.Configure();
}
protected void Application_Error(object sender, EventArgs e)
{
Exception ex = Server.GetLastError().GetBaseException();
log4net.ILog log = log4net.LogManager.GetLogger("LogServer");
log.Error(ex);
}
2)web.config中进行配置
a)将<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>放到<configSections>中。
<configSections>
<!--错误日志配置-->
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
b)将<log4net>..................</log4net>放到</system.web>之后, <system.codedom>之前。
<!--错误日志配置-->
<log4net>
<appender name="TraceAppender" type="log4net.Appender.TraceAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline"/>
</layout>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline"/>
</layout>
</appender>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="Log\\SysLog-"/>
<!--日志文件夹及文件名开头-->
<appendToFile value="true"/>
<!--是否追加到文件-->
<RollingStyle value="Date"/>
<!--日期的格式-->
<DatePattern value="yyyy-MM-dd".txt""/>
<!--文件名后面加上.log后缀,必须使用转义字符-->
<maximumFileSize value="1MB"/>
<!--最大文件大小-->
<StaticLogFileName value="false"/>
<!--日志文件名是否为静态-->
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline"/>
<param name="Header" value=" --------------------------网站运行过程中产生的错误--------------------------"/>
<param name="Footer" value=" ----------------------------------管理系统---------------------------------"/>
</layout>
</appender>
<logger name="LogServer">
<level value="All"/>
</logger>
<root>
<appender-ref ref="TraceAppender"/>
<appender-ref ref="ConsoleAppender"/>
<appender-ref ref="RollingFileAppender"/>
</root>
</log4net>