1.在專案中安裝log4net 包
2.增加配置檔案:
1 <?xml version="1.0" encoding="utf-8"?> 2 <configuration> 3 <configSections> 4 <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" /> 5 </configSections> 6 <log4net> 7 <logger name="run"> 8 <appender-ref ref="MessageAppender" /> 9 </logger> 10 <appender name="MessageAppender" type="log4net.Appender.RollingFileAppender"> 11 <param name="Encoding" value="utf-8" /> 12 <file value=".\logs\RunLog\" /> 13 <appendToFile value="true" /> 14 <maxSizeRollBackups value="24" /> 15 <maximumFileSize value="100MB" /> 16 <rollingStyle velue="Date" /> 17 <datePattern value="yyyy-MM-dd-HH".txt"" /> 18 <staticLogFileName value="false" /> 19 <layout type="log4net.Layout.PatternLayout"> 20 <conversionPattern value="%date{yyyy-MM-dd HH:mm:ss fff}$%-5level%message%newline" /> 21 </layout> 22 </appender> 23 <logger name="error_record"> 24 <appender-ref ref="ErrorRecordAppender" /> 25 </logger> 26 <appender name="ErrorRecordAppender" type="log4net.Appender.RollingFileAppender"> 27 <param name="Encoding" value="utf-8" /> 28 <file value=".\logs\ErrorRecord\" /> 29 <appendToFile value="true" /> 30 <maxSizeRollBackups value="24" /> 31 <maximumFileSize value="500MB" /> 32 <rollingStyle velue="Date" /> 33 <datePattern value="yyyy-MM-dd-HH".txt"" /> 34 <staticLogFileName value="false" /> 35 <layout type="log4net.Layout.PatternLayout"> 36 <conversionPattern value="%date{yyyy-MM-dd HH:mm:ss};%message%newline" /> 37 </layout> 38 </appender> 39 <logger name="operation"> 40 <appender-ref ref="OperationAppender" /> 41 </logger> 42 <appender name="OperationAppender" type="log4net.Appender.RollingFileAppender"> 43 <param name="Encoding" value="utf-8" /> 44 <file value=".\logs\OperationLog\" /> 45 <appendToFile value="true" /> 46 <maxSizeRollBackups value="24" /> 47 <maximumFileSize value="100MB" /> 48 <rollingStyle velue="Date" /> 49 <datePattern value="yyyy-MM-dd".txt"" /> 50 <staticLogFileName value="false" /> 51 <layout type="log4net.Layout.PatternLayout"> 52 <conversionPattern value="%date{HH:mm:ss fff}$%-5level$%message$%newline" /> 53 </layout> 54 </appender> 55 <logger name="webapi"> 56 <appender-ref ref="webAppender" /> 57 </logger> 58 <appender name="webAppender" type="log4net.Appender.RollingFileAppender"> 59 <param name="Encoding" value="utf-8" /> 60 <file value=".\logs\WebApi\" /> 61 <appendToFile value="true" /> 62 <maxSizeRollBackups value="24" /> 63 <maximumFileSize value="100MB" /> 64 <rollingStyle velue="Date" /> 65 <datePattern value="yyyy-MM-dd".txt"" /> 66 <staticLogFileName value="false" /> 67 <layout type="log4net.Layout.PatternLayout"> 68 <conversionPattern value="%date{HH:mm:ss fff}$%-5level$%message$%newline" /> 69 </layout> 70 </appender> 71 </log4net> 72 </configuration>
3.將該配置檔案設定為【始終複製】:
4.在程式中啟動入口載入:
1 private static readonly ILog log = LogManager.GetLogger("run"); 2 /// <summary> 3 /// 應用程式的主入口點。 4 /// </summary> 5 [STAThread] 6 static void Main() 7 { 8 var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log4Net.config"); 9 XmlConfigurator.Configure(new FileInfo(path)); 10 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); 11 Application.ThreadException += Application_ThreadException; 12 AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; 13 Application.EnableVisualStyles(); 14 Application.SetCompatibleTextRenderingDefault(false); 15 Application.Run(new HELLERMonitor1()); 16 } 17 18 private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) 19 { 20 log.Error(e); 21 } 22 23 private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e) 24 { 25 log.Error(e); 26 }
5.在專案中根據日誌分類呼叫輸出:
1 private static readonly ILog log = LogManager.GetLogger("run"); 2 public Form1() 3 { 4 InitializeComponent(); 5 log.Debug("程式執行中。。。"); 6 }
6.檢視執行結果: