每作一個新項目,都會用到log4net,但老是忘記如何快速配置。有時在網上搜半天也找不到好的模板,大都在介紹參數的使用,在此作下總結,爭取下次用時僅10分鐘就可搭建好log4net。web
直接上介紹的步驟:apache
1. 官網下載最新版的log4net。http://logging.apache.org/log4net/download_log4net.cgi,打開官網,在Binaries類別下,下載最新版的log4net包,點擊以下圖所示按鈕下載:app
2. 找到所需的log4net版本。下載後,解壓縮包,找到本身.net程序所對應的log4net版本,我用的是VisualStudio 2010,項目用的是.netframework 4.0, 因此我用的log4net版本在C:\Users\zhengshuangliang\Downloads\log4net-1.2.13-bin-newkey\log4net-1.2.13\bin\net\4.0\release\框架
3. 選擇配置信息存放方式。我喜歡把log4net的配置文件和項目的配置文件(app.config或web.config)放在一塊兒,因此我刪除了log4net自帶的配置文件log4net.xml,而是把配置信息放在app.config或web.config裏。ide
4. 在最終執行的項目配置文件裏添加log4net配置信息。在app.config或web.config中添加以下面代碼中的<configSections>和<log4net>的內容(代碼中以黃色背景標識)。工具
只需在最終執行的項目下添加配置文件就行,由於全部的其餘項目或引用的dll都會走同一個入口(可執行項目),因此整個產品就只有一份log4net配置文件便可,在你想寫日誌的項目裏引用log4net.dll,直接建立Logger對象寫日誌便可,詳細操做可參照步驟6。ui
如下log4net配置信息將會在可執行的項目目錄下建立Log文件夾,裏面會建立名位:Logs_20150116.txt;Logs_20150116.txt1;Logs_20150116.txt2的日誌文件,每一個文件最大爲10M,不設文件數目上限,日期變化後會從新生成新的日誌文件。若須要更詳細的的配置信息請參看其餘博文,本文不作詳細介紹this
<?xml version="1.0"?> <configuration> <configSections> <section name="log4net" type="System.Configuration.IgnoreSectionHandler"/> </configSections> <appSettings> <add key="RestServicePort" value="8899"/> </appSettings> <log4net> <!--配置日誌的級別,低於此級別的就不寫到日誌裏面去OFF、FATAL、ERROR, WARN, INFO, DEBUG, ALL--> <root> <level value="DEBUG" /> <appender-ref ref="LogFileAppender" /> <appender-ref ref="ConsoleAppender" /> </root> <logger name="WebLogger"> <level value="ERROR" /> </logger> <!--輸出到文件--> <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender,log4net"> <param name="File" value="Log/" /> <param name="AppendToFile" value="true" /> <!-- 切割最多文件數 -1表示不限制產生日誌文件數--> <param name="MaxSizeRollBackups" value="-1"/> <!-- 每一個文件的大小限制 --> <param name="MaximumFileSize" value="10MB"/> <!-- RollingStyle Composite 綜合 Size 按大小 Date 按時間 --> <param name="RollingStyle" value="Composite" /> <param name="DatePattern" value=""Logs_"yyyyMMdd".txt"" /> <param name="StaticLogFileName" value="false" /> <layout type="log4net.Layout.PatternLayout,log4net"> <param name="Header" value="
----------------------application startup--------------------------
" /> <param name="Footer" value="
 ----------------------application shutdown--------------------------
" /> <param name="ConversionPattern" value="%d ThreadId[%t] %-5p %c - (Line:%line) %m%n" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="ALL" /> <param name="LevelMax" value="OFF" /> </filter> </appender> <!--輸出控制檯--> <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender,log4net"> <layout type="log4net.Layout.PatternLayout,log4net"> <param name="ConversionPattern" value="%d [%t] %-5p %c - (%line) %m%n" /> </layout> </appender> </log4net> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> </startup> </configuration>
5. 配置可執行項目的AssemblyInfo.cs文件,以便讓log4net的配置信息讓全部dll找到。在可執行項目的AssemblyInfo.cs文件中添加以下代碼中的最後一行spa
using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information // associated with an assembly. [assembly: AssemblyTitle("***CentralServiceStartEngine")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("IGT Technology Development (Beijing) Co., Ltd.")] [assembly: AssemblyProduct("***CentralServiceStartEngine")] [assembly: AssemblyCopyright("Copyright ©***, Ltd. 2014")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] // Setting ComVisible to false makes the types in this assembly not visible // to COM components. If you need to access a type in this assembly from // COM, set the ComVisible attribute to true on that type. [assembly: ComVisible(false)] // The following GUID is for the ID of the typelib if this project is exposed to COM [assembly: Guid("7c36bff0-c***-4f43-8be8-3781390c35c7")] // Version information for an assembly consists of the following four values: // // Major Version // Minor Version // Build Number // Revision // // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] [assembly: AssemblyVersion("1.0.0.0")] [assembly: AssemblyFileVersion("1.0.0.0")] [assembly: log4net.Config.XmlConfigurator(Watch = true)]
6.項目中使用Log4net。把log4net.dll(無需log4net.xml)放在整個項目的Lib工具庫裏,在須要寫日誌的項目中引用此log4net.dll文件,首先建立Logger,而後直接寫日誌便可。以下代碼:.net
using System; using System.Collections.Generic; using System.Configuration; using System.Text; using log4net; namespace ***.***.*** { public static class CentralServiceEngine { //CentralServiceEngine將會在日誌中體現,爲的是更快定位問題 private static ILog _log = LogManager.GetLogger(typeof (CentralServiceEngine)); public static void StartService() { _log.Info("In OnStart IPSCentralService."); _log.Info("Info"); _log.Debug("Debug"); _log.Warn("Warn"); _log.Error("ERROR"); _log.Fatal("Fatal"); } public static void StopService() { _log.Info("In onStop IPSCentralService."); } } }
Build代碼若出現找不到log4net命名空間或程序集的錯誤,則修改下項目的Target Framework(目標框架,在項目->右鍵屬相->Application頁籤中),從.NET Framework 4 Client Profile 變爲.NET Framework 4。
以上代碼中會有帶*號的內容,目的是不想把本身的內容公開,用時請替換爲你的真實數據。
此博文僅爲能快速搭建寫日誌的平臺,有問題隨時交流,錯誤之處歡迎批評指正。