首页 > 代码库 > LOG4NET用法(个人比较喜欢的用法)(转)
LOG4NET用法(个人比较喜欢的用法)(转)
web.config配置如下:
[xhtml] view plaincopy
- <?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <configSections>
- <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
- </configSections>
- <log4net>
- <logger name="logerror">
- <level value=http://www.mamicode.com/"ERROR" />
- <appender-ref ref="ErrorAppender" />
- </logger>
- <logger name="loginfo">
- <level value=http://www.mamicode.com/"INFO" />
- <appender-ref ref="InfoAppender" />
- </logger>
- <appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender">
- <param name="File" value=http://www.mamicode.com/"Log//LogError//" />
- <param name="AppendToFile" value=http://www.mamicode.com/"true" />
- <param name="MaxSizeRollBackups" value=http://www.mamicode.com/"100" />
- <param name="MaxFileSize" value=http://www.mamicode.com/"10240" />
- <param name="StaticLogFileName" value=http://www.mamicode.com/"false" />
- <param name="DatePattern" value=http://www.mamicode.com/"yyyyMMdd".htm"" />
- <param name="RollingStyle" value=http://www.mamicode.com/"Date" />
- <layout type="log4net.Layout.PatternLayout">
- <param name="ConversionPattern" value="http://www.mamicode.com/<HR COLOR=red>%n异常时间:%d [%t] <BR>%n异常级别:%-5p
- <BR>%n异 常 类:%c [%x] <BR>%n%m <BR>%n <HR Size=1>" />
- </layout>
- </appender>
- <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
- <param name="File" value=http://www.mamicode.com/"Log//LogInfo//" />
- <param name="AppendToFile" value=http://www.mamicode.com/"true" />
- <param name="MaxFileSize" value=http://www.mamicode.com/"10240" />
- <param name="MaxSizeRollBackups" value=http://www.mamicode.com/"100" />
- <param name="StaticLogFileName" value=http://www.mamicode.com/"false" />
- <param name="DatePattern" value=http://www.mamicode.com/"yyyyMMdd".htm"" />
- <param name="RollingStyle" value=http://www.mamicode.com/"Date" />
- <layout type="log4net.Layout.PatternLayout">
- <param name="ConversionPattern" value="http://www.mamicode.com/<HR COLOR=blue>%n日志时间:%d [%t] <BR>%n日志级别:%-5p
- <BR>%n日 志 类:%c [%x] <BR>%n%m <BR>%n <HR Size=1>" />
- </layout>
- </appender>
- </log4net>
- <system.web>
- <compilation defaultLanguage="c#" debug="true" />
- <customErrors mode="RemoteOnly" />
- <authentication mode="Windows" />
- <authorization>
- <allow users="*" />
- </authorization>
- <trace enabled="false" requestLimit="10" pageOutput="false" traceMode="SortByTime" localOnly="true" />
- <sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes"
- cookieless="false" timeout="20" />
- <globalization requestEncoding="utf-8" responseEncoding="utf-8" />
- </system.web>
- </configuration>
LOG操作类
[c-sharp] view plaincopy
- using System;
- using System.IO;
- namespace TankAction.SystemLog
- {
- /**//// <summary>
- /// LogHelper的摘要说明。
- /// </summary>
- public class LogHelper
- {
- private SystemLog()
- {
- }
- public static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo");
- public static readonly log4net.ILog logerror = log4net.LogManager.GetLogger("logerror");
- public static void SetConfig()
- {
- log4net.Config.DOMConfigurator.Configure();
- }
- public static void SetConfig(FileInfo configFile)
- {
- log4net.Config.DOMConfigurator.Configure(configFile);
- }
- public static void WriteLog(string info)
- {
- if(loginfo.IsInfoEnabled)
- {
- loginfo.Info(info);
- }
- }
- public static void WriteLog(string info,Exception se)
- {
- if(logerror.IsErrorEnabled)
- {
- logerror.Error(info,se);
- }
- }
- }
- }
Global.asax.cs文件配置如下:
[c-sharp] view plaincopy
- protected void Application_Start(Object sender, EventArgs e)
- {
- SystemLog.SetConfig();
- }
- protected void Application_Error(Object sender, EventArgs e)
- {
- Exception objExp = HttpContext.Current.Server.GetLastError();
- LogHelper.WriteLog("/r/n客户机IP:"+ Request.UserHostAddress +"/r/n错误地址:"+ Request.Url +"/r/n异常信息:"+ Server.GetLastError().Message,objExp);
- }
eg:
try
{}
catch(Exception ex)
{
LogHelper.WriteLog("ErrorInfo"ex);
}
LOG4NET用法(个人比较喜欢的用法)(转)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。