标签:
首先添加对log4net.dll的引用
1、Web.config
<configuration>的<configSections>节点下新增(要在头部):
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
<configuration>根节点下新增:
<log4net> <!-- OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL --> <!-- Set root logger level to ERROR and its appenders --> <root> <level value="ERROR" /><!--日志输出级别--> <appender-ref ref="RollingFileTracer" /> </root> <!-- Print only messages of level DEBUG or above in the packages --> <appender name="RollingFileTracer" type="log4net.Appender.RollingFileAppender,log4net"> <!--RollingFileAppender日志记录形式,还有邮箱和FTP形式,可以将错误信息发给管理员-->
<param name="File" value="App_Data/Log/" /><!--日志保存路径--> <param name="AppendToFile" value="true" /><!--是否附加文件--> <param name="RollingStyle" value="Date" /><!--根据时间滚动--> <param name="MaxSizeRollBackups" value="10" /><!--最大日志文件数量--> <param name="MaximumFileSize" value="1MB" /><!--最大日志文件大小--> <param name="DatePattern" value=""Logs_"yyyyMMdd".txt"" /> <param name="StaticLogFileName" value="false" /> <layout type="log4net.Layout.PatternLayout,log4net"> <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" /> </layout> </appender> </log4net>
3、测试使用
ILog logger = LogManager.GetLogger(typeof(类名));
logger.Error("要记录的错误消息");
4、Application_Error中记录未处理异常
protected void Application_Error(object sender, EventArgs e)
{
ILog log = LogManager.GetLogger(typeof(Global));
log.Error("系统发生未处理异常",Context.Error);
}
标签:
原文地址:http://www.cnblogs.com/sean100/p/4680539.html