代碼先行-log4Net初體驗

來源:互聯網
上載者:User

標籤:

1、安裝

  從http://logging.apache.org/log4net/download_log4net.cgi下載編譯好的log4Net包並解壓。

  找到 bin\net\4.0\release\log4net.dll,在項目中添加引用。

  我下載的最新版是log4net 1.2.13。

2、配置  在項目的Propeties/AssemblyInfo.cs中的最後一行添加代碼,用於設定log4net的設定檔,Watch表示是否監聽設定檔的變動。
1 [assembly: log4net.Config.DOMConfigurator( Watch = true)]
  配置App.Confog,在configuration節點內,開始的地方聲明log4Net自訂節,內容如下:
1 <configSections>2     <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net-net-1.2.13.0" />3 </configSections>

  緊跟著添加自訂節的配置詳情:

 1 <log4net> 2     <root> 3       <level value="ALL" /> 4       <appender-ref ref="FileAppender" /> 5     </root> 6  7     <appender name="FileAppender" type="log4net.Appender.FileAppender" > 8       <param name="File" value="log.txt" /> 9       <param name="AppendToFile" value="true" />10       <layout type="log4net.Layout.PatternLayout">11         <param name="ConversionPattern" value="%n%d [%t] %-5p %c [%x]  - %m%n" />12       </layout>13     </appender>14 </log4net>    
3、使用
 1 static void Main(string[] args) 2 { 3     int x = 21; 4     int y = 0; 5  6     try 7     { 8         int z = x / y; 9     }10     catch (Exception ex)11     {12         ILog logger = log4net.LogManager.GetLogger("log");13         logger.Error("被除數不能為0!", ex);14     }15 }
最後:驗證

  開啟bin\Debug\log.txt。

 

代碼先行-log4Net初體驗

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.