zoukankan      html  css  js  c++  java
  • LOG4NET用法

    log 4net组件下载:log4net.dll

    web.config配置如下:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
     <configSections>
      <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
     </configSections>
     <log4net>
      <logger name="logerror">
       <level value="ERROR" />
       <appender-ref ref="ErrorAppender" />
      </logger>
      <logger name="loginfo">
       <level value="INFO" />
       <appender-ref ref="InfoAppender" />
      </logger>
      <appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender">
       <param name="File" value="Log\LogError\" />
       <param name="AppendToFile" value="true" />
       <param name="MaxSizeRollBackups" value="100" />
       <param name="MaxFileSize" value="10240" />
       <param name="StaticLogFileName" value="false" />
       <param name="DatePattern" value="yyyyMMdd&quot;.htm&quot;" />
       <param name="RollingStyle" value="Date" />
       <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="&lt;HR COLOR=red&gt;%n异常时间:%d [%t] &lt;BR&gt;%n异常级别:%-5p 
       &lt;BR&gt;%n异 常 类:%c [%x] &lt;BR&gt;%n%m &lt;BR&gt;%n &lt;HR Size=1&gt;" />
       </layout>
      </appender>
      <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
       <param name="File" value="Log\LogInfo\" />
       <param name="AppendToFile" value="true" />
       <param name="MaxFileSize" value="10240" />
       <param name="MaxSizeRollBackups" value="100" />
       <param name="StaticLogFileName" value="false" />
       <param name="DatePattern" value="yyyyMMdd&quot;.htm&quot;" />
       <param name="RollingStyle" value="Date" />
       <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="&lt;HR COLOR=blue&gt;%n日志时间:%d [%t] &lt;BR&gt;%n日志级别:%-5p 
       &lt;BR&gt;%n日 志 类:%c [%x] &lt;BR&gt;%n%m &lt;BR&gt;%n &lt;HR Size=1&gt;" />
       </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>
    View Code

    LOG操作类:

    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);
                }
            }
        }
    }
    View Code

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

    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("
    客户机IP:"+ Request.UserHostAddress +"
    错误地址:"+ Request.Url +"
    异常信息:"+ Server.GetLastError().Message,objExp);
      }
    View Code

    eg:
       try
       {}
       catch(Exception ex)
       {
           LogHelper.WriteLog("ErrorInfo"ex);
       }

  • 相关阅读:
    【JZOJ3743】【BZOJ5158】Alice and Bob
    【JZOJ3719】K-D-Sequence
    【JZOJ1913】【BZOJ2124】等差子序列
    【JZOJ1914】【BZOJ2125】最短路
    【luoguP4768】【NOI2018】归程
    【JZOJ6435】【luoguP5666】【CSP-S2019】树的重心
    【JZOJ6434】【luoguP5665】【CSP-S2019】划分
    【JZOJ6433】【luoguP5664】【CSP-S2019】Emiya 家今天的饭
    【JZOJ6431】【luoguP5658】【CSP-S2019】括号树
    【JZOJ3673】【luoguP4040】【BZOJ3874】宅男计划
  • 原文地址:https://www.cnblogs.com/tianciliangen/p/4303371.html
Copyright © 2011-2022 走看看