1.NuGet包管理器中安裝log4net 2.接著在項目中添加log4net.config文件,以及手擼或尋找一個LogHelper 添加完log4net.config一定要設置屬性,將其始終複製到輸出目錄 3.最後需要在AssemblyInfo.cs中添加一行代碼 效果如下: Demo下載:ht ...
1.NuGet包管理器中安裝log4net
2.接著在項目中添加log4net.config文件,以及手擼或尋找一個LogHelper
添加完log4net.config一定要設置屬性,將其始終複製到輸出目錄
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections> <log4net xsi:noNamespaceSchemaLocation="http://csharptest.net/downloads/schema/log4net.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <root> <level value="DEBUG"/> <appender-ref ref="ErrorRollingFileAppender"/> <appender-ref ref="WarnRollingFileAppender"/> <appender-ref ref="InfoRollingFileAppender"/> <appender-ref ref="DebugRollingFileAppender"/> </root> <!--一般錯誤日誌定義,用於記錄已知需處理的與未捕獲的異常--> <!--日誌輸出格式:[時間]:類名 線程號 消息--> <appender name="ErrorRollingFileAppender" type="log4net.Appender.RollingFileAppender"> <filter type="log4net.Filter.LevelRangeFilter"> <levelMin value="ERROR"/> <levelMax value="FATAL"/> </filter> <filter type="log4net.Filter.DenyAllFilter"/> <file value="logs\"/> <rollingStyle value="Date"/> <datePattern value="yyyy-MM-dd\\"Error.log""/> <staticLogFileName value="false"/> <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="[%d{yyyy-MM-dd HH:mm:ss.fff}] %-5p %c.%M %t %n%m%n"/> </layout> </appender> <!--警告日誌定義,用於記錄已知不需處理的異常,系統警告信息--> <!--日誌輸出格式:[時間]:類名 線程號 消息--> <appender name="WarnRollingFileAppender" type="log4net.Appender.RollingFileAppender"> <filter type="log4net.Filter.LevelMatchFilter"> <levelToMatch value="WARN"/> </filter> <filter type="log4net.Filter.DenyAllFilter"/> <file value="logs\"/> <appendToFile value="true"/> <rollingStyle value="Date"/> <datePattern value="yyyy-MM-dd\\"Warn.log""/> <staticLogFileName value="false"/> <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="[%d{yyyy-MM-dd HH:mm:ss.fff}] %c.%M %t %m%n"/> </layout> </appender> <!--信息日誌定義,用於記錄用戶相關信息--> <!--日誌輸出格式:[時間]:消息--> <appender name="InfoRollingFileAppender" type="log4net.Appender.RollingFileAppender"> <filter type="log4net.Filter.LevelMatchFilter"> <levelToMatch value="INFO"/> </filter> <filter type="log4net.Filter.DenyAllFilter"/> <file value="logs\"/> <appendToFile value="true"/> <rollingStyle value="Date"/> <datePattern value="yyyy-MM-dd\\"Info.log""/> <staticLogFileName value="false"/> <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="[%d{yyyy-MM-dd HH:mm:ss}] %m%n"/> </layout> </appender> <!--信息日誌定義,用於收集開發調試信息--> <!--日誌輸出格式:[時間]:類名 線程號 消息--> <appender name="DebugRollingFileAppender" type="log4net.Appender.RollingFileAppender"> <filter type="log4net.Filter.LevelMatchFilter"> <levelToMatch value="DEBUG"/> </filter> <filter type="log4net.Filter.DenyAllFilter"/> <file value="logs\"/> <appendToFile value="true"/> <rollingStyle value="Date"/> <datePattern value="yyyy-MM-dd\\"Debug.log""/> <staticLogFileName value="false"/> <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="[%d{yyyy-MM-dd HH:mm:ss.fff}] %c %t:%m%n"/> </layout> </appender> </log4net> </configuration>
1 public static class LogHelper 2 { 3 private static readonly log4net.ILog Logerror = log4net.LogManager.GetLogger("DEBUG"); 4 5 public static void WriteInfoLog(string info) 6 { 7 Logerror.Info(info); 8 } 9 10 public static void WriteErrorLog(string info) 11 { 12 Logerror.Error(info); 13 } 14 15 public static void WriteDebugLog(string info) 16 { 17 Logerror.Debug(info); 18 } 19 20 public static void WriteDebugLog(Exception exception) 21 { 22 Logerror.Debug(exception.Message, exception); 23 } 24 }
3.最後需要在AssemblyInfo.cs中添加一行代碼
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", ConfigFileExtension = "config", Watch = true)]
效果如下:
1 static void Main(string[] args) 2 { 3 LogHelper.WriteDebugLog("1234444445"); 4 }
Demo下載:https://pan.baidu.com/s/1hFXoUPZUWVCwDdjx-bTtHQ