其餘配置詳情在網上均可以找到,可是不少看着都暈,本人就記錄一下如何使用:app
一、按不一樣級別(官方說明)可記錄的日誌級別有:spa
Info、Warn、Error、Debug線程
二、能夠按着四個配置四個輸出日誌路徑和配置文件的配置節點日誌
能夠在filter節點中配置value來區別記錄日誌的級別code
例如:xml
<appender name="Errorlog" type="log4net.Appender.RollingFileAppender"> <!--定義文件存放位置--> <file value="Errorlog\\"/> <appendToFile value="true"/> <rollingStyle value="Date"/> <datePattern value="yyyyMMdd'.txt'"/> <staticLogFileName value="false"/> <param name="MaxSizeRollBackups" value="100"/> <layout type="log4net.Layout.PatternLayout"> <!--每條日誌末尾的文字說明 輸出格式 樣例:2008-03-26 13:42:32,111 [10] INFO Log4NetDemo.MainClass [(null)] - info <conversionPattern value="%newline %n記錄時間:%date %n線程ID:[%thread] %n日誌級別: %-5level %n出錯類:%logger property: [%property{NDC}] - %n錯誤描述:%message%newline %n"/>--> <conversionPattern value="%date 描述:%message%n"/> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="ERROR"/> <param name="LevelMax" value="ERROR"/> </filter> </appender>
而後能夠根據本身須要配置不一樣的輸出路徑,注意filter裏的param節點的value必須大寫並只能是四種節點的一種valueblog
最終能夠在logger節點中配置指向appenderutf-8
<logger name="myLogger">
<level value="ALL"/>
<appender-ref ref="Errorlog"/>
<appender-ref ref="Runlog"/>
<appender-ref ref="Warnlog"/>
<appender-ref ref="Debuglog"/>
<!--<appender-ref ref="ErrorLoging" />-->
</logger>string
最後附上配置文件以及LogHelper類:it
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> </configSections> <log4net> <!--定義輸出到文件中--> <appender name="Errorlog" type="log4net.Appender.RollingFileAppender"> <!--定義文件存放位置--> <file value="Errorlog\\"/> <appendToFile value="true"/> <rollingStyle value="Date"/> <datePattern value="yyyyMMdd'.txt'"/> <staticLogFileName value="false"/> <param name="MaxSizeRollBackups" value="100"/> <layout type="log4net.Layout.PatternLayout"> <!--每條日誌末尾的文字說明 輸出格式 樣例:2008-03-26 13:42:32,111 [10] INFO Log4NetDemo.MainClass [(null)] - info <conversionPattern value="%newline %n記錄時間:%date %n線程ID:[%thread] %n日誌級別: %-5level %n出錯類:%logger property: [%property{NDC}] - %n錯誤描述:%message%newline %n"/>--> <conversionPattern value="%date 描述:%message%n"/> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="ERROR"/> <param name="LevelMax" value="ERROR"/> </filter> </appender> <appender name="Runlog" type="log4net.Appender.RollingFileAppender"> <!--定義文件存放位置--> <file value="Runlog\\"/> <appendToFile value="true"/> <rollingStyle value="Date"/> <datePattern value="yyyyMMdd'.txt'"/> <staticLogFileName value="false"/> <param name="MaxSizeRollBackups" value="100"/> <layout type="log4net.Layout.PatternLayout"> <!--每條日誌末尾的文字說明 輸出格式 樣例:2008-03-26 13:42:32,111 [10] INFO Log4NetDemo.MainClass [(null)] - info <conversionPattern value="%newline %n記錄時間:%date %n線程ID:[%thread] %n日誌級別: %-5level %n出錯類:%logger property: [%property{NDC}] - %n錯誤描述:%message%newline %n"/>--> <conversionPattern value="%date 描述:%message%n"/> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="INFO"/> <param name="LevelMax" value="INFO"/> </filter> </appender> <appender name="Warnlog" type="log4net.Appender.RollingFileAppender"> <!--定義文件存放位置--> <file value="Warnlog\\"/> <appendToFile value="true"/> <rollingStyle value="Date"/> <datePattern value="yyyyMMdd'.txt'"/> <staticLogFileName value="false"/> <param name="MaxSizeRollBackups" value="100"/> <layout type="log4net.Layout.PatternLayout"> <!--每條日誌末尾的文字說明 輸出格式 樣例:2008-03-26 13:42:32,111 [10] INFO Log4NetDemo.MainClass [(null)] - info <conversionPattern value="%newline %n記錄時間:%date %n線程ID:[%thread] %n日誌級別: %-5level %n出錯類:%logger property: [%property{NDC}] - %n錯誤描述:%message%newline %n"/>--> <conversionPattern value="%date 描述:%message%n"/> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="WARN"/> <param name="LevelMax" value="WARN"/> </filter> </appender> <appender name="Debuglog" type="log4net.Appender.RollingFileAppender"> <!--定義文件存放位置--> <file value="Debuglog\\"/> <appendToFile value="true"/> <rollingStyle value="Date"/> <datePattern value="yyyyMMdd'.txt'"/> <staticLogFileName value="false"/> <param name="MaxSizeRollBackups" value="100"/> <layout type="log4net.Layout.PatternLayout"> <!--每條日誌末尾的文字說明 輸出格式 樣例:2008-03-26 13:42:32,111 [10] INFO Log4NetDemo.MainClass [(null)] - info <conversionPattern value="%newline %n記錄時間:%date %n線程ID:[%thread] %n日誌級別: %-5level %n出錯類:%logger property: [%property{NDC}] - %n錯誤描述:%message%newline %n"/>--> <conversionPattern value="%date 描述:%message%n"/> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="DEBUG"/> <param name="LevelMax" value="DEBUG"/> </filter> </appender> <logger name="myLogger"> <level value="ALL"/> <appender-ref ref="Errorlog"/> <appender-ref ref="Runlog"/> <appender-ref ref="Warnlog"/> <appender-ref ref="Debuglog"/> <!--<appender-ref ref="ErrorLoging" />--> </logger> <!--<root> <level value="ALL"/> --> <!--文件形式記錄日誌--> <!-- <appender-ref ref="SuoerServerLog"/> <appender-ref ref="PDFWatcherLog"/> </root>--> </log4net> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> </startup> </configuration>
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; [assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)] namespace DotNet.Utilities.Log { public class LogHelper { //配置loger指向 static log4net.ILog log = log4net.LogManager.GetLogger("myLogger"); /// <summary> /// 輸出日誌到Log4Net /// </summary> /// <param name="t"></param> /// <param name="msg"></param> #region Error節點 public static void WriteErrorLog(string msg) { //log4net.ILog log = log4net.LogManager.GetLogger(t); log.Error(msg); } #endregion #region Info節點 public static void WriteInfoLog(string msg) { //log4net.ILog log = log4net.LogManager.GetLogger(t); log.Info(msg); } #endregion #region Debug節點 public static void WriteDebugLog(string msg) { //log4net.ILog log = log4net.LogManager.GetLogger(t); log.Debug(msg); } #endregion #region Warn節點 public static void WriteWarnLog(string msg) { //log4net.ILog log = log4net.LogManager.GetLogger(t); log.Warn(msg); } #endregion } }