一、log4net下载:http://logging.apache.org/log4net/download_log4net.cgi
二、web.config配置如下:
1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <configSections> 4 <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" /> 5 </configSections> 6 <log4net> 7 <logger name="logerror"> 8 <level value="ERROR" /> 9 <appender-ref ref="ErrorAppender" /> 10 </logger> 11 <logger name="loginfo"> 12 <level value="INFO" /> 13 <appender-ref ref="InfoAppender" /> 14 </logger> 15 <appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender"> 16 <param name="File" value="Log\\LogError\\" /> 17 <param name="AppendToFile" value="true" /> 18 <param name="MaxSizeRollBackups" value="100" /> 19 <param name="MaxFileSize" value="10240" /> 20 <param name="StaticLogFileName" value="false" /> 21 <param name="DatePattern" value="yyyyMMdd".htm"" /> 22 <param name="RollingStyle" value="Date" /> 23 <layout type="log4net.Layout.PatternLayout"> 24 <param name="ConversionPattern" value="<HR COLOR=red>%n异常时间:%d [%t] <BR>%n异常级别:%-5p 25 <BR>%n异 常 类:%c [%x] <BR>%n%m <BR>%n <HR Size=1>" /> 26 </layout> 27 </appender> 28 <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender"> 29 <param name="File" value="Log\\LogInfo\\" /> 30 <param name="AppendToFile" value="true" /> 31 <param name="MaxFileSize" value="10240" /> 32 <param name="MaxSizeRollBackups" value="100" /> 33 <param name="StaticLogFileName" value="false" /> 34 <param name="DatePattern" value="yyyyMMdd".htm"" /> 35 <param name="RollingStyle" value="Date" /> 36 <layout type="log4net.Layout.PatternLayout"> 37 <param name="ConversionPattern" value="<HR COLOR=blue>%n日志时间:%d [%t] <BR>%n日志级别:%-5p 38 <BR>%n日 志 类:%c [%x] <BR>%n%m <BR>%n <HR Size=1>" /> 39 </layout> 40 </appender> 41 </log4net> 42 <system.web> 43 <compilation defaultLanguage="c#" debug="true" /> 44 <customErrors mode="RemoteOnly" /> 45 <authentication mode="Windows" /> 46 <authorization> 47 <allow users="*" /> 48 </authorization> 49 <trace enabled="false" requestLimit="10" pageOutput="false" traceMode="SortByTime" localOnly="true" /> 50 <sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes" 51 cookieless="false" timeout="20" /> 52 <globalization requestEncoding="utf-8" responseEncoding="utf-8" /> 53 </system.web> 54 </configuration>
三、LOG操作类:
1 using System; 2 using System.IO; 3 4 namespace SystemLog 5 { 6 /// <summary> 7 /// LogHelper的摘要说明。 8 /// </summary> 9 public class LogHelper 10 { 11 12 public static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo"); 13 14 public static readonly log4net.ILog logerror = log4net.LogManager.GetLogger("logerror"); 15 16 public static void SetConfig() 17 { 18 log4net.Config.XmlConfigurator.Configure(); 19 } 20 21 public static void SetConfig(FileInfo configFile) 22 { 23 log4net.Config.XmlConfigurator.Configure(configFile); 24 } 25 26 public static void WriteLog(string info) 27 { 28 if(loginfo.IsInfoEnabled) 29 { 30 loginfo.Info(info); 31 } 32 } 33 34 public static void WriteLog(string info,Exception se) 35 { 36 if(logerror.IsErrorEnabled) 37 { 38 logerror.Error(info,se); 39 } 40 } 41 } 42 }
四、Global.asax.cs文件配置如下:
1 <%@ Application Language="C#" %> 2 <%@ Import Namespace="SystemLog" %> 3 4 <script runat="server"> 5 6 void Application_Start(object sender, EventArgs e) 7 { 8 // 在应用程序启动时运行的代码 9 LogHelper.SetConfig(); 10 } 11 12 void Application_End(object sender, EventArgs e) 13 { 14 // 在应用程序关闭时运行的代码 15 16 } 17 18 void Application_Error(object sender, EventArgs e) 19 { 20 // 在出现未处理的错误时运行的代码 21 Exception objExp = HttpContext.Current.Server.GetLastError(); 22 LogHelper.WriteLog("客户机IP:" + Request.UserHostAddress + "<br/>错误地址:" + Request.Url + "<br/>异常信息:" + Server.GetLastError().Message, objExp); 23 } 24 25 void Session_Start(object sender, EventArgs e) 26 { 27 // 在新会话启动时运行的代码 28 29 } 30 31 void Session_End(object sender, EventArgs e) 32 { 33 // 在会话结束时运行的代码。 34 // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为 InProc 时,才会引发 Session_End 事件。 35 // 如果会话模式设置为 StateServer 36 // 或 SQLServer,则不会引发该事件。 37 38 } 39 40 </script>
五、页级别处理:
1 try 2 { 3 string str = Request.QueryString["a"].ToString(); 4 } 5 catch (Exception ex) 6 { 7 LogHelper.WriteLog("ErrorInfo", ex); 8 }
六、效果:
本文参考原文:http://fanrsh.cnblogs.com/archive/2006/06/08/420546.html
时间: 2024-10-28 21:27:32