1. 程式人生 > 實用技巧 >C#中四步輕鬆使用log4net記錄本地日誌的方法

C#中四步輕鬆使用log4net記錄本地日誌的方法

在這裡,記錄我在專案中使用log4net記錄本地日誌的步驟。在不會之前感覺很難,很神祕,一旦會了之後其實沒那麼難。其實所有的事情都是一樣的,下面我就分享一下我使用log4Net的經驗。

第一步:首先從Visual Studio中的Nuget包管理中搜索下載 Log4Net dll檔案 如下圖:

選擇安裝的專案(哪個類庫中需要記錄日誌就勾選上)

第二步:開啟配置檔案 WinFrom就是 App.config Web就是 web.config 將以下配置資訊加入

  1 <configSections>
  2  <section name="log4net" type="
log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> 3 </configSections> 4 <log4net> 5 <logger name="SysRFLogger"> 6 <level value="DEBUG" /> 7 <appender-ref ref="SysRFAppender" /> 8 </logger> 9 <logger name="DebugRFLogger"> 10 <level value="
DEBUG" /> 11 <appender-ref ref="DebugAppender" /> 12 </logger> 13 <logger name="MsgLogger"> 14 <level value="DEBUG" /> 15 <appender-ref ref="MsgAppender" /> 16 </logger> 17 <logger name="OperInfoLogger"> 18 <level value="DEBUG" /> 19 <appender-ref
ref="OperInfoAppender" /> 20 </logger> 21 <appender name="SysRFAppender" type="log4net.Appender.RollingFileAppender"> 22 <file value="Logs\syslog.log" /> 23 <appendToFile value="true" /> 24 <rollingStyle value="Date" /> 25 <preserveLogFileNameExtension value="true" /> 26 <datePattern value="yyyyMMdd" /> 27 <layout type="log4net.Layout.PatternLayout"> 28 <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> 29 </layout> 30 </appender> 31 <appender name="DebugAppender" type="log4net.Appender.RollingFileAppender"> 32 <file value="Logs\sysDebuglog.log" /> 33 <appendToFile value="true" /> 34 <rollingStyle value="Date" /> 35 <preserveLogFileNameExtension value="true" /> 36 <datePattern value="yyyyMMdd" /> 37 <layout type="log4net.Layout.PatternLayout"> 38 <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> 39 </layout> 40 </appender> 41 <appender name="MsgAppender" type="log4net.Appender.RollingFileAppender"> 42 <file value="Logs\Messagelog.log" /> 43 <appendToFile value="true" /> 44 <rollingStyle value="Date" /> 45 <preserveLogFileNameExtension value="true" /> 46 <datePattern value="yyyyMMdd" /> 47 <layout type="log4net.Layout.PatternLayout"> 48 <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> 49 </layout> 50 </appender> 51 <appender name="OperInfoAppender" type="log4net.Appender.AdoNetAppender"> 52 <bufferSize value="1" /> 53 <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=4.0.30319.18020, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> 54 <commandText value="INSERT INTO LOG_USEROPERLOG ([ID],[Date],[User],[Flag],[OperInfo],[OperFlag],[OperResult],[Message]) VALUES (@Id,@log_date, @user, @flag, @operinfo,@operflag, @operresult, @message)" /> 55 <parameter> 56 <parameterName value="@Id" /> 57 <dbType value="String" /> 58 <size value="36" /> 59 <layout type="NT.MultithreadingTaskService.Log.Layout.UserOperLayOut,NT.MultithreadingTaskService.Log"> 60 <param name="ConversionPattern" value="%property{ID}" /> 61 </layout> 62 </parameter> 63 <parameter> 64 <parameterName value="@log_date" /> 65 <dbType value="DateTime" /> 66 <layout type="log4net.Layout.RawTimeStampLayout" /> 67 </parameter> 68 <parameter> 69 <parameterName value="@user" /> 70 <dbType value="String" /> 71 <size value="64" /> 72 <layout type="NT.MultithreadingTaskService.Log.Layout.UserOperLayOut,NT.MultithreadingTaskService.Log"> 73 <param name="ConversionPattern" value="%property{User}" /> 74 </layout> 75 </parameter> 76 <parameter> 77 <parameterName value="@flag" /> 78 <dbType value="Int32" /> 79 <layout type="NT.MultithreadingTaskService.Log.Layout.UserOperLayOut,NT.MultithreadingTaskService.Log"> 80 <param name="ConversionPattern" value="%property{Flag}" /> 81 </layout> 82 </parameter> 83 <parameter> 84 <parameterName value="@operinfo" /> 85 <dbType value="String" /> 86 <size value="255" /> 87 <layout type="NT.MultithreadingTaskService.Log.Layout.UserOperLayOut,NT.MultithreadingTaskService.Log"> 88 <param name="ConversionPattern" value="%property{OperInfo}" /> 89 </layout> 90 </parameter> 91 <parameter> 92 <parameterName value="@operflag" /> 93 <dbType value="String" /> 94 <size value="32" /> 95 <layout type="NT.MultithreadingTaskService.Log.Layout.UserOperLayOut,NT.MultithreadingTaskService.Log"> 96 <param name="ConversionPattern" value="%property{OperFlag}" /> 97 </layout> 98 </parameter> 99 <parameter> 100 <parameterName value="@operresult" /> 101 <dbType value="String" /> 102 <size value="255" /> 103 <layout type="NT.MultithreadingTaskService.Log.Layout.UserOperLayOut,NT.MultithreadingTaskService.Log"> 104 <param name="ConversionPattern" value="%property{OperResult}" /> 105 </layout> 106 </parameter> 107 <parameter> 108 <parameterName value="@message" /> 109 <dbType value="String" /> 110 <size value="4000" /> 111 <layout type="NT.MultithreadingTaskService.Log.Layout.UserOperLayOut,NT.MultithreadingTaskService.Log"> 112 <param name="ConversionPattern" value="%property{Message}" /> 113 </layout> 114 </parameter> 115 </appender> 116 <root> 117 <level value="DEBUG" /> 118 <appender-ref ref="SysRFAppender" /> 119 </root> 120 </log4net>

以上節點具體引數這裡不做解釋,可以上網查詢幫助文件

注意:這兩段XML要放在configuration 節點下靠前

第三步:開啟專案AssemblyInfo.cs檔案(UI層)

加入:[assembly: log4net.Config.XmlConfigurator(ConfigFile = "web.config", Watch = true)]

第四步:在需要記錄日誌的cs檔案中初始化: private ILog _log = LogManager.GetLogger("TicketFace");

一般常用以下幾個方法:

_log.Error();
_log.Debug();
_log.Info();

最後執行專案然後在以上配置檔案中找到路徑去資料夾裡看看就生成了日誌檔案了。

注意:這裡日誌是以天為單位記錄 如下: