一、在webconfig裏添加配置web
1.1 在<configSections> 中添加節點數據庫
<configuration>
<configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/> </configSections>
</configuration>
1.2 添加log4net配置sectionapp
<log4net> <appender name="fatalAppender" type="log4net.Appender.RollingFileAppender"> <filter type="log4net.Filter.LevelMatchFilter"> <levelToMatch value="FATAL" /> </filter> <filter type="log4net.Filter.DenyAllFilter" /> <File value="Logs\fatal.log" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <datePattern value="yyyyMMdd" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" /> </layout> </appender> <appender name="errorAppender" type="log4net.Appender.RollingFileAppender"> <filter type="log4net.Filter.LevelMatchFilter"> <levelToMatch value="ERROR" /> </filter> <filter type="log4net.Filter.DenyAllFilter" /> <File value="Logs\err.log" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <datePattern value="yyyyMMdd" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" /> </layout> </appender> <appender name="warnAppender" type="log4net.Appender.RollingFileAppender"> <filter type="log4net.Filter.LevelMatchFilter"> <levelToMatch value="WARN" /> </filter> <filter type="log4net.Filter.DenyAllFilter" /> <File value="Logs\warn.log" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <datePattern value="yyyyMMdd" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" /> </layout> </appender> <appender name="infoAppender" type="log4net.Appender.RollingFileAppender"> <filter type="log4net.Filter.LevelMatchFilter"> <levelToMatch value="INFO" /> </filter> <filter type="log4net.Filter.DenyAllFilter" /> <File value="Logs\info.log" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <datePattern value="yyyyMMdd" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" /> </layout> </appender> <appender name="debugAppender" type="log4net.Appender.RollingFileAppender"> <filter type="log4net.Filter.LevelMatchFilter"> <levelToMatch value="DEBUG" /> </filter> <filter type="log4net.Filter.DenyAllFilter" /> <File value="Logs\debug.log" /> <!--這裏能夠配置日誌存放位置--> <appendToFile value="true" /> <rollingStyle value="Date" /> <datePattern value="yyyyMMdd" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" /> </layout> </appender> <appender name="perfAppender" type="log4net.Appender.RollingFileAppender"> <filter type="log4net.Filter.LevelMatchFilter"> <levelToMatch value="INFO" /> </filter> <filter type="log4net.Filter.DenyAllFilter" /> <File value="Logs\perf.log" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <datePattern value="yyyyMMdd" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date %logger - %message%newline" /> </layout> </appender> <root> <level value="ALL" /> <appender-ref ref="fatalAppender" /> <appender-ref ref="errorAppender" /> <appender-ref ref="warnAppender" /> <appender-ref ref="infoAppender" /> <appender-ref ref="debugAppender" /> </root> </log4net>
二、在Global.asax.cs裏添加this
log4net.Config.XmlConfigurator.Configure();spa
三、添加 helper類debug
using log4net; using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace TestLog4Net { public class log4netHelper { public static void Fatal(Type type, object message, Exception exception = null) { ILog log = LogManager.GetLogger(type); if (exception == null) log.Fatal(message); else log.Fatal(message, exception); } public static void Error(Type type, object message, Exception exception = null) { ILog log = LogManager.GetLogger(type); if (exception == null) log.Error(message); else log.Error(message, exception); } public static void Warn(Type type, object message, Exception exception = null) { ILog log = LogManager.GetLogger(type); if (exception == null) log.Warn(message); else log.Warn(message, exception); } public static void Info(Type type, object message, Exception exception = null) { ILog log = LogManager.GetLogger(type); if (exception == null) log.Info(message); else log.Info(message, exception); } public static void Debug(Type type, object message, Exception exception = null) { ILog log = LogManager.GetLogger(type); if (exception == null) log.Debug(message); else log.Debug(message, exception); } } }
四、代碼裏使用3d
ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);//獲取一個日誌記錄器 log4netHelper.Fatal(this.GetType(), "Fatal"); //致命錯誤 log4netHelper.Error(this.GetType(), "Error", new Exception("error")); //錯誤 log4netHelper.Warn(this.GetType(), "Warn"); //禁告 log4netHelper.Info(this.GetType(), "Info"); //信息 log4netHelper.Debug(this.GetType(), "Debug"); //調試
FATAL(致命錯誤):記錄系統中出現的能使用系統徹底失去功能,服務中止,系統崩潰等使系統沒法繼續運行下去的錯誤。例如,數據庫沒法鏈接,系統出現死循環。調試
ERROR(通常錯誤):記錄系統中出現的致使系統不穩定,部分功能出現混亂或部分功能失效一類的錯誤。例如,數據字段爲空,數據操做不可完成,操做出現異常等。日誌
WARN(警告):記錄系統中不影響系統繼續運行,但不符合系統運行正常條件,有可能引發系統錯誤的信息。例如,記錄內容爲空,數據內容不正確等。code
INFO(通常信息):記錄系統運行中應該讓用戶知道的基本信息。
DEBUG (調試信息):記錄系統用於調試的一切信息,內容或者是一些關鍵數據內容的輸出。
一、在以上基礎上 配置修改
<log4net> <!-- OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL --> <!-- Set root logger level to ERROR and its appenders --> <root> <level value="ALL" /> <appender-ref ref="SysAppender" /> </root> <!-- Print only messages of level DEBUG or above in the packages --> <logger name="WebLogger"> <level value="log" /> </logger> <appender name="SysAppender" type="log4net.Appender.RollingFileAppender,log4net"> <!--<param name="File" value="App_Data/" />--> <File value="Logs\log" /> <!--日誌文件位置和文件名--> <param name="AppendToFile" value="true" /> <param name="RollingStyle" value="Date" /> <!--<param name="DatePattern" value=""Logs_"yyyyMMdd".txt"" />--> <param name="DatePattern" value="yyyyMMdd".txt"" /><!--在文件名後面加內容 好比 log名變爲log20180808--> <param name="StaticLogFileName" value="false" /> <layout type="log4net.Layout.PatternLayout,log4net"> <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" /> </layout> </appender> <appender name="consoleApp" type="log4net.Appender.ConsoleAppender,log4net"> <layout type="log4net.Layout.PatternLayout,log4net"> <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" /> </layout> </appender> </log4net>
二、代碼使用
ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);//獲取一個日誌記錄器
log.Fatal("info");
log.Error("Error");
log.Warn("Warn");
log.Info("info");
log.Debug("Debug");