首页 > 代码库 > 利用log4添加log

利用log4添加log

應用log4net.dll

新建Global.asax,在cs文件中添加

 protected void Application_Start(object sender, EventArgs e)
        {
            #region system log init
            log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(AppDomain.CurrentDomain.BaseDirectory + @"\Log.config"));
            #endregion
        }

 

添加Log.config文件

技术分享
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    </configSections>

    <log4net>
        <root>
            <level value=http://www.mamicode.com/"Error"/>
        </root>
        <logger name="FatalLog">
            <level value=http://www.mamicode.com/"Fatal"/>
            <appender-ref ref="RollingLogFileAppender"/>
        </logger>
        <logger name="ErrorLog">
            <level value=http://www.mamicode.com/"Error"/>
            <appender-ref ref="RollingLogFileAppender"/>
        </logger>
        <logger name="WarnLog">
            <level value=http://www.mamicode.com/"Warn"/>
            <appender-ref ref="RollingLogFileAppender"/>
        </logger>
        <logger name="DebugLog">
            <level value=http://www.mamicode.com/"Debug"/>
            <appender-ref ref="RollingLogFileAppender"/>
        </logger>
        <logger name="HTGInfoLog">
            <level value=http://www.mamicode.com/"Info"/>
            <appender-ref ref="RollingHTGFileAppender"/>
        </logger>
        <logger name="F4GInfoLog">
            <level value=http://www.mamicode.com/"Info"/>
            <appender-ref ref="RollingF4GFileAppender"/>
        </logger>
        <logger name="InfoLog">
            <level value=http://www.mamicode.com/"Info"/>
            <appender-ref ref="RollingLogFileAppender"/>
        </logger>
    <logger name="RuleEngineLog">
      <level value=http://www.mamicode.com/"Info"/>
      <appender-ref ref="RollingRuleEngineLogFileAppender"/>
    </logger>
        
        <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
            <!--輸出路徑-->
            <file value=http://www.mamicode.com/"Log\"/>
            <param name="AppendToFile" value=http://www.mamicode.com/"true"/>
            <param name="MaxSizeRollBackups" value=http://www.mamicode.com/"1000"/>
            <param name="StaticLogFileName" value=http://www.mamicode.com/"false"/>
            <!--輸出按指定方式切換文件-->
            <param name="DatePattern" value=http://www.mamicode.com/"yyyyMMdd&quot;.log&quot;"/>
            <param name="RollingStyle" value=http://www.mamicode.com/"Composite"/><!--Date 按日期,Size 按大小-->
      <param name="maximumFileSize" value=http://www.mamicode.com/"10240KB" />
      <!--輸出LOG格式-->
            <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
            <layout type="log4net.Layout.PatternLayout">
                <param name="Header" value=http://www.mamicode.com/""/>
                <param name="ConversionPattern" value=http://www.mamicode.com/"%n------------LogName:%c --------------------------------------------%nTimestamp: %d %nClient: %X%nThread:%thread%nLevel: %p%n%m %n-------------------------------------------------------------------------%n"/>
                <param name="Footer" value=http://www.mamicode.com/""/>
            </layout>
            <!--輸出不同級別的LOG-->
            <!--<filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value=http://www.mamicode.com/"DEBUG"/>

        <param name="LevelMax" value=http://www.mamicode.com/"FATAL"/>

      </filter>-->
        </appender>

    <appender name="RollingRuleEngineLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <!--Output path-->
      <file value=http://www.mamicode.com/"RuleEngineLog\"/>
      <param name="AppendToFile" value=http://www.mamicode.com/"true"/>
      <param name="MaxSizeRollBackups" value=http://www.mamicode.com/"1000"/>
      <param name="StaticLogFileName" value=http://www.mamicode.com/"false"/>
      <!--Output file as specified switch-->
      <param name="DatePattern" value=http://www.mamicode.com/"&quot;RuleEngineLog&quot;yyyyMMdd&quot;.log&quot;"/>
      <param name="RollingStyle" value=http://www.mamicode.com/"Composite"/>
      <param name="maximumFileSize" value=http://www.mamicode.com/"10240KB" />
      <!--LOG output format-->
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="Header" value=http://www.mamicode.com/""/>
        <param name="ConversionPattern" value=http://www.mamicode.com/"%n------------LogName:%c--------------------------------------------%n%m %n-----------------------------------------------------------------------------%n"/>
        <param name="Footer" value=http://www.mamicode.com/""/>
      </layout>
    </appender>
    </log4net>
</configuration>
View Code

 

利用log4添加log