Apache log4net 配置的部分解釋

來源:互聯網
上載者:User

標籤:winform   style   blog   http   color   使用   

log4net 其實可以使用 系統內建的 *.config 設定檔 來配置,也可以使用 其他設定檔 來配置。

這兩種配置方式決定了 log4net 使用哪些方式來讀取配置資訊,從而也決定了一些特性。

log4net 用來讀取設定檔的方式有兩種,一種是 ASP.NET 的 System.Configuration API,另一種是 log4net 的 log4net.Config.XmlConfigurator。


那麼他們的區別在哪裡呢?

System.Configuration API:

1、只能使用系統內建的 *.config 設定檔來配置(WinForm 是 XXX.exe.config,WebForm 是 web.config)。

(「因為 System.Configuration API 不能重新載入設定檔」,此乃英文文檔的直譯,我估計他的意思是,設定檔只能從系統內建的 *.config 設定檔一次性讀取,不能進行二次讀取,所以就不能使用其他設定檔來配置了,就算配置了也讀取不了。)

2、不能監控設定檔的變動。意思就是,你的程式一旦運行著,你修改設定檔(比如:由寫入文字檔改為寫入資料庫)是沒有作用的。

(這點可以從上面一點的解釋那裡可以知道,因為 System.Configuration API 不能重新載入設定檔。)

3、讀取配置許可權跟著 ASP.NET 或者 IIS,所以配置應用程式許可權的時候,完全不用擔心設定檔的許可權問題。


log4net.Config.XmlConfigurator:

1、與上面相反,可以使用系統內建的 *.config 設定檔來配置,也可以使用其他設定檔來配置。

2、與上面相反,可以進行監控,有改動立馬生效。

3、與上面相反,絕對要搞清楚程式是否有許可權讀取檔案。

那兩種方式應該怎麼配置呢?

System.Configuration API:

 1 <?xml version="1.0" encoding="utf-8"?> 2 <configuration> 3     <configSections> 4         <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> 5         <!--這一段很重要,它的意思就是,下面 log4net 這一個 section 使用後面這個程式來處理,這個程式其實就是調用 System.Configuration API,所以聲明這一段就是代表使用 System.Configuration API。--> 6     </configSections> 7     <log4net> 8         <!--這裡是一些具體配置--> 9     </log4net>10 </configuration>
web.config
 1 [XmlConfigurator] 2 //[XmlConfigurator(ConfigFile = "xxx.config")] //不可用。 3 //[XmlConfigurator(ConfigFileExtension = "config")] //不可用。 4 //[XmlConfigurator(Watch = true)] //不可用。 5 class Program 6 { 7     static void Main(string[] args) 8     { 9         //下面這兩個可用,二選一。10         XmlConfigurator.Configure();//這一條等同於 [XmlConfigurator]。11         XmlConfigurator.Configure(log4net.LogManager.GetRepository("DEBUG"));12 13         //下面這個是停用。14         //XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(@"~\xxx.config")); //不可用。15     }16 }
Log4netDemo.cs


log4net.Config.XmlConfigurator:

1、使用其他設定檔配置。

如果使用其他設定檔來配置的話,那麼系統內建的 *.config 設定檔就不用添加任何東西,直接建立一個設定檔(比如:xxx.config),裡面只要有 log4net 這個 section 就可以。

1 <?xml version="1.0" encoding="utf-8"?>2 <log4net>3     <!--這裡是一些具體配置-->4 </log4net>
xxx.config

2、使用系統內建的 *.config 設定檔配置。

 1 <?xml version="1.0" encoding="utf-8"?> 2 <configuration> 3     <configSections> 4         <section name="log4net" type="System.Configuration.IgnoreSectionHandler" /> 5         <!--因為這是系統的設定檔,所以裡面的每個 section 都要正確,但是下面 log4net 對於系統來說是不正確的,如果不寫這一段會報錯,這一段的意思是忽略 log4net 這個 section 的意思。--> 6     </configSections> 7     <log4net> 8         <!--這裡是一些具體配置--> 9     </log4net>10 </configuration>
web.config
 1 //下面的都可用。 2 [XmlConfigurator]//使用系統內建的 *.config 設定檔。 3 [XmlConfigurator(ConfigFile = "xxx.config")]//使用其他設定檔配置。 4 [XmlConfigurator(ConfigFileExtension = "config")]//使用其他設定檔配置。 5 [XmlConfigurator(Watch = true)]//監控設定檔。 6 class Program 7 { 8     static void Main(string[] args) 9     {10         //下面的都可用。11         XmlConfigurator.Configure();//這一條等同於 [XmlConfigurator] 聲明。12         XmlConfigurator.Configure(log4net.LogManager.GetRepository("DEBUG"));13 14         XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(@"~\xxx.config"));//這一條等同於 [XmlConfigurator(ConfigFile = "xxx.config", Watch = true)] 聲明。15     }16 }
Log4netDemo.cs


上面這些代碼只是一種展示,裡面的文法是錯誤的,應該完全理解之後,結合自己的項目進行配搭。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.