Log4Net使用

一、Log4Net官網apache

二、Web使用:app

     Web.Config 添加:      ui

   

<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net debug="false">
    <root>
      <level value="Error" />
      <appender-ref ref="UserInfoLogger" />
    </root>
    <logger name="UserInfoLogger">
      <!--日誌的等級,它們由高到底分別爲:
OFF > FATAL > ERROR > WARN > INFO > DEBUG  > ALL 
注意:日誌記錄時只記錄當前及更高等級日誌。LevelRangeFilter節點存在時以節點爲配置爲準-->
      <level value="DEBUG" />
      <!--Appender(輸出流)名稱-->
      <appender-ref ref="UserInfoLogger" />
    </logger>
    <appender name="UserInfoLogger" type="log4net.Appender.RollingFileAppender,log4net">
      <!--日誌文件目錄-->
      <param name="File" value="UserInfoLogger/" />
      <!--寫入方式爲追加-->
      <param name="AppendToFile" value="true" />
      <!--日誌回滾方式爲日期-->
      <param name="RollingStyle" value="Date" />
      <Encoding value="UTF-8" />
      <!--日誌文件命名方式-->
      <param name="DatePattern" value="&quot;Logs_&quot;yyyyMMddHH&quot;.txt&quot;" />
      <!--日誌名稱-->
      <param name="StaticLogFileName" value="false" />
      <!--最多記錄日誌文件數-->
      <param name="MaxSizeRollBackups" value="10" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="5MB" />
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <layout type="log4net.Layout.PatternLayout,log4net">
        <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
      </layout>
      <!--日誌級別限制-->
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="DEBUG" />
        <param name="LevelMax" value="WARN" />
      </filter>
    </appender>
  </log4net>

 

Global.asax 添加spa

  log4net.Config.XmlConfigurator.Configure();.net

三、添加類庫debug

 

public class Log4NetHelper
    {
        private static log4net.ILog log = log4net.LogManager.GetLogger("UserInfoLogger");

        public static void WriteLogDebug(string message)
        {
            //啓用Debug
            if (log.IsDebugEnabled)
            {
                log.Debug($"{DateTime.Now}:Debug:{message}");
            }

            if (log.IsErrorEnabled)
            {
                log.Error($"{DateTime.Now}:Error:{message}");
            }

            if (log.IsFatalEnabled)
            {
                log.Fatal($"{DateTime.Now}:Fatal:{message}");
            }

            if (log.IsInfoEnabled)
            {
                log.Info($"{DateTime.Now}:Info:{message}");
            }


        }
    }

 

參考資料:日誌

 一、官網:http://logging.apache.org/log4net/code

二、Csdn_阿齊茲_很是完善的Log4net詳細說明blog

相關文章
相關標籤/搜索