log4net
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了log4net相关的知识,希望对你有一定的参考价值。
log4net.Config.XmlConfigurator.Configure();//获取Log4Net配置信息(配置信息定义在Web.config文件中) ThreadPool.QueueUserWorkItem(o => { while (true) { try { if (ErrorHanderAttribute.qe.Count > 0) { Exception e = ErrorHanderAttribute.qe.Dequeue(); if (e != null) { ILog logger = LogManager.GetLogger("Log"); logger.Error(e);//将异常信息写到Log4Net中. } else { Thread.Sleep(100); } } Thread.Sleep(100); } catch (Exception e) { ErrorHanderAttribute.qe.Enqueue(e); } } });
public class ErrorHanderAttribute : System.Web.Mvc.HandleErrorAttribute { public static Queue<Exception> qe = new Queue<Exception>(); public override void OnException(ExceptionContext filterContext) { qe.Enqueue(filterContext.Exception); filterContext.HttpContext.Response.Redirect("/home/err"); base.OnException(filterContext); } }
<log4net> <!-- OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL --> <!-- Set root logger level to ERROR and its appenders --> <root> <level value="ALL"/> <appender-ref ref="SysAppender"/> </root> <!-- Print only messages of level DEBUG or above in the packages --> <logger name="WebLogger"> <level value="DEBUG"/> </logger> <appender name="SysAppender" type="log4net.Appender.RollingFileAppender,log4net" > <param name="File" value="App_Data/" /> <param name="AppendToFile" value="true" /> <param name="RollingStyle" value="Date" /> <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" /> <param name="Header" value=" ----------------------header-------------------------- " /> <param name="Footer" value=" ----------------------footer-------------------------- " /> </layout> </appender> <appender name="consoleApp" type="log4net.Appender.ConsoleAppender,log4net"> <layout type="log4net.Layout.PatternLayout,log4net"> <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" /> </layout> </appender> </log4net>
以上是关于log4net的主要内容,如果未能解决你的问题,请参考以下文章