​log 4net组件下载:log4net.dll​


web.config配置如下:


LOG4NET用法_xml<?

xml version = " 1.0 "  encoding = " utf-8 "   ?>

LOG4NET用法_xml

< configuration >

LOG4NET用法_xml 

< configSections >

LOG4NET用法_xml  

< section name = " log4net "  type = " log4net.Config.Log4NetConfigurationSectionHandler,log4net "   />

LOG4NET用法_xml 

</ configSections >

LOG4NET用法_xml 

< log4net >

LOG4NET用法_xml  

< logger name = " logerror " >

LOG4NET用法_xml   

< level value = " ERROR "   />

LOG4NET用法_xml   

< appender - ref   ref = " ErrorAppender "   />

LOG4NET用法_xml  

</ logger >

LOG4NET用法_xml  

< logger name = " loginfo " >

LOG4NET用法_xml   

< level value = " INFO "   />

LOG4NET用法_xml   

< appender - ref   ref = " InfoAppender "   />

LOG4NET用法_xml  

</ logger >

LOG4NET用法_xml  

< appender name = " ErrorAppender "  type = " log4net.Appender.RollingFileAppender " >

LOG4NET用法_xml   

< param name = " File "  value = " Log//LogError// "   />

LOG4NET用法_xml   

< param name = " AppendToFile "  value = " true "   />

LOG4NET用法_xml   

< param name = " MaxSizeRollBackups "  value = " 100 "   />

LOG4NET用法_xml   

< param name = " MaxFileSize "  value = " 10240 "   />

LOG4NET用法_xml   

< param name = " StaticLogFileName "  value = " false "   />

LOG4NET用法_xml   

< param name = " DatePattern "  value = " yyyyMMdd&quot;.htm&quot; "   />

LOG4NET用法_xml   

< param name = " RollingStyle "  value = " Date "   />

LOG4NET用法_xml   

< layout type = " log4net.Layout.PatternLayout " >

LOG4NET用法_xml    

< param name = " ConversionPattern "  value = " &lt;HR COLOR=red&gt;%n异常时间:%d [%t] &lt;BR&gt;%n异常级别:%-5p 

LOG4NET用法_xml

    & lt;BR & gt; % n异 常 类: % c [ % x]  & lt;BR & gt; % n % m  & lt;BR & gt; % n  & lt;HR Size = 1 & gt; "  />

LOG4NET用法_xml

    </ layout >

LOG4NET用法_xml  

</ appender >

LOG4NET用法_xml  

< appender name = " InfoAppender "  type = " log4net.Appender.RollingFileAppender " >

LOG4NET用法_xml   

< param name = " File "  value = " Log//LogInfo// "   />

LOG4NET用法_xml   

< param name = " AppendToFile "  value = " true "   />

LOG4NET用法_xml   

< param name = " MaxFileSize "  value = " 10240 "   />

LOG4NET用法_xml   

< param name = " MaxSizeRollBackups "  value = " 100 "   />

LOG4NET用法_xml   

< param name = " StaticLogFileName "  value = " false "   />

LOG4NET用法_xml   

< param name = " DatePattern "  value = " yyyyMMdd&quot;.htm&quot; "   />

LOG4NET用法_xml   

< param name = " RollingStyle "  value = " Date "   />

LOG4NET用法_xml   

< layout type = " log4net.Layout.PatternLayout " >

LOG4NET用法_xml    

< param name = " ConversionPattern "  value = " &lt;HR COLOR=blue&gt;%n日志时间:%d [%t] &lt;BR&gt;%n日志级别:%-5p 

LOG4NET用法_xml

    & lt;BR & gt; % n日 志 类: % c [ % x]  & lt;BR & gt; % n % m  & lt;BR & gt; % n  & lt;HR Size = 1 & gt; "  />

LOG4NET用法_xml

    </ layout >

LOG4NET用法_xml  

</ appender >

LOG4NET用法_xml 

</ log4net >

LOG4NET用法_xml 

< system.web >

LOG4NET用法_xml  

< compilation defaultLanguage = " c# "  debug = " true "   />

LOG4NET用法_xml  

< customErrors mode = " RemoteOnly "   />

LOG4NET用法_xml  

< authentication mode = " Windows "   />

LOG4NET用法_xml  

< authorization >

LOG4NET用法_xml   

< allow users = " * "   />

LOG4NET用法_xml  

</ authorization >

LOG4NET用法_xml  

< trace enabled = " false "  requestLimit = " 10 "  pageOutput = " false "  traceMode = " SortByTime "  localOnly = " true "   />

LOG4NET用法_xml  

< sessionState mode = " InProc "  stateConnectionString = " tcpip=127.0.0.1:42424 "  sqlConnectionString = " data source=127.0.0.1;Trusted_Connection=yes "

LOG4NET用法_xml   cookieless

= " false "  timeout = " 20 "   />

LOG4NET用法_xml  

< globalization requestEncoding = " utf-8 "  responseEncoding = " utf-8 "   />

LOG4NET用法_xml 

</ system.web >

LOG4NET用法_xml

</ configuration >

LOG操作类


LOG4NET用法_xmlusing

 System;

LOG4NET用法_xml

using  System.IO;

LOG4NET用法_xml

LOG4NET用法_xml

namespace  TankAction.SystemLog

LOG4NET用法_c_59LOG4NET用法_log4net_60

LOG4NET用法_xml_61

{

LOG4NET用法_xml_62LOG4NET用法_encoding_63    /**//// <summary>

LOG4NET用法_encoding_64    /// LogHelper的摘要说明。

LOG4NET用法_exception_65    /// </summary>

LOG4NET用法_encoding_64    public class LogHelper

LOG4NET用法_xml_62LOG4NET用法_encoding_63    LOG4NET用法_xml_61{

LOG4NET用法_encoding_64        private SystemLog()

LOG4NET用法_xml_62LOG4NET用法_encoding_63        LOG4NET用法_xml_61{

LOG4NET用法_exception_65        }

LOG4NET用法_encoding_64

LOG4NET用法_encoding_64        public static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo");

LOG4NET用法_encoding_64

LOG4NET用法_encoding_64        public static readonly log4net.ILog logerror = log4net.LogManager.GetLogger("logerror");

LOG4NET用法_encoding_64

LOG4NET用法_encoding_64        public static void SetConfig()

LOG4NET用法_xml_62LOG4NET用法_encoding_63        LOG4NET用法_xml_61{

LOG4NET用法_encoding_64            log4net.Config.DOMConfigurator.Configure();

LOG4NET用法_exception_65        }

LOG4NET用法_encoding_64

LOG4NET用法_encoding_64        public static void SetConfig(FileInfo configFile)

LOG4NET用法_xml_62LOG4NET用法_encoding_63        LOG4NET用法_xml_61{

LOG4NET用法_encoding_64            log4net.Config.DOMConfigurator.Configure(configFile); 

LOG4NET用法_exception_65        }

LOG4NET用法_encoding_64

LOG4NET用法_encoding_64        public static void WriteLog(string info)

LOG4NET用法_xml_62LOG4NET用法_encoding_63        LOG4NET用法_xml_61{

LOG4NET用法_encoding_64            if(loginfo.IsInfoEnabled)

LOG4NET用法_xml_62LOG4NET用法_encoding_63            LOG4NET用法_xml_61{

LOG4NET用法_encoding_64                loginfo.Info(info);

LOG4NET用法_exception_65            }

LOG4NET用法_exception_65        }

LOG4NET用法_encoding_64

LOG4NET用法_encoding_64        public static void WriteLog(string info,Exception se)

LOG4NET用法_xml_62LOG4NET用法_encoding_63        LOG4NET用法_xml_61{

LOG4NET用法_encoding_64            if(logerror.IsErrorEnabled)

LOG4NET用法_xml_62LOG4NET用法_encoding_63            LOG4NET用法_xml_61{

LOG4NET用法_encoding_64                logerror.Error(info,se);

LOG4NET用法_exception_65            }

LOG4NET用法_exception_65        }

LOG4NET用法_exception_65    }

LOG4NET用法_log4net_118}


LOG4NET用法_xml



Global.asax.cs文件配置如下:


LOG4NET用法_xml  

protected   void  Application_Start(Object sender, EventArgs e)

LOG4NET用法_c_59LOG4NET用法_log4net_60  

LOG4NET用法_xml_61

{

LOG4NET用法_encoding_64            SystemLog.SetConfig();

LOG4NET用法_log4net_118  }


LOG4NET用法_xml  

protected   void  Application_Error(Object sender, EventArgs e)

LOG4NET用法_c_59LOG4NET用法_log4net_60  

LOG4NET用法_xml_61

{

LOG4NET用法_encoding_64   Exception objExp = HttpContext.Current.Server.GetLastError();

LOG4NET用法_encoding_64   LogHelper.WriteLog("/r/n客户机IP:"+ Request.UserHostAddress +"/r/n错误地址:"+ Request.Url +"/r/n异常信息:"+ Server.GetLastError().Message,objExp);

LOG4NET用法_log4net_118  }


eg:

   try

   {}

   catch(Exception ex)

   {

       LogHelper.WriteLog("ErrorInfo"ex);

   }