log4net 使用教程

來源:互聯網
上載者:User

標籤:style   color   使用   檔案   io   for   art   cti   

1.下載log4net (Google log4net) //已有



2.unzip log4net 

3.運行VS,建立 c# Windows應用程式。 

4.添加引用Log4NET 

5.建立一個應用程式設定檔App.config(具體內容附在後面) 

6.開啟Form1.cs, 

在Namespace上添加一行 [assembly: log4net.Config.DOMConfigurator(Watch=true)] 
(或者 編輯Assembly.cs檔案,添加如下內容: 
[assembly:log4net.Config.DOMConfigurator( ConfigFileExtension="config",Watch=true)] ) 

也可以在properties下面的AssemblyInfo.cs下添加

[assembly:log4net.Config.XOMConfigurator( ConfigFileExtension="config",Watch=true)] )  


在類Form1中添加一個靜態變數 

private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); 



7.添加一個按鈕。在按鈕處理函數中添加一行 log.Warn("你好!"); 



8.運行程式。點一下按鈕。 



OK,開啟Bin\Debug\log-file.txt,可以看到“你好”。 





附.App.config 



<?xml version="1.0" encoding="utf-8" ?> 

<configuration> 

<!-- Register a section handler for the log4net section --> 

<configSections> 

<section name="log4net" type="System.Configuration.IgnoreSectionHandler" /> 

</configSections> 

<appSettings> 

<!-- To enable internal log4net logging specify the following appSettings key --> 

<!-- <add key="log4net.Internal.Debug" value="true"/> --> 

</appSettings> 

<!-- This section contains the log4net configuration settings --> 

<log4net> 

<!-- Define some output appenders --> 

<appender name="LogFileAppender" type="log4net.Appender.FileAppender"> 

<param name="File" value="log-file.txt" /> 

<!-- Example using environment variables in params --> 

<!-- <param name="File" value="${TMP}\\log-file.txt" /> --> 

<param name="AppendToFile" value="true" /> 

<!-- An alternate output encoding can be specified --> 

<!-- <param name="Encoding" value="unicodeFFFE" /> --> 

<layout type="log4net.Layout.PatternLayout"> 

<param name="Header" value="[Header]\r\n" /> 

<param name="Footer" value="[Footer]\r\n" /> 

<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] <%X{auth}> - %m%n" /> 

</layout> 

<!-- Alternate layout using XML 

<layout type="log4net.Layout.XMLLayout" /> --> 

</appender> 



<!-- Setup the root category, add the appenders and set the default level --> 

<root> 

<level value="ALL" /> 

<appender-ref ref="LogFileAppender" /> 

<!-- <appender-ref ref="A" /> --> 

</root> 



<!-- Specify the level for some specific categories --> 

<logger name="SLog4net.Form1"> 

<!-- <appender-ref ref="B" /> --> 

<level value="ALL" /> 

<appender-ref ref="RollingLogFileAppender" /> 

</logger> 

</log4net> 

</configuration> 





--------------------------------App.config 

<?xml version="1.0" encoding="utf-8" ?> 

<configuration> 

<!-- Register a section handler for the log4net section --> 

<configSections> 

<section name="log4net" type="System.Configuration.IgnoreSectionHandler" /> 

</configSections> 

<appSettings> 

<!-- To enable internal log4net logging specify the following appSettings key --> 

<!-- <add key="log4net.Internal.Debug" value="true"/> --> 

</appSettings> 

<!-- This section contains the log4net configuration settings --> 

<log4net> 

<!-- Define some output appenders --> 

<appender name="LogFileAppender" type="log4net.Appender.FileAppender"> 

<param name="File" value="log-file.txt" /> 

<!-- Example using environment variables in params --> 

<!-- <param name="File" value="${TMP}\\log-file.txt" /> --> 

<param name="AppendToFile" value="true" /> 

<!-- An alternate output encoding can be specified --> 

<!-- <param name="Encoding" value="unicodeFFFE" /> --> 

<layout type="log4net.Layout.PatternLayout"> 

<param name="Header" value="[Header]\r\n" /> 

<param name="Footer" value="[Footer]\r\n" /> 

<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] <%X{auth}> - %m%n" /> 

</layout> 

<!-- Alternate layout using XML 

<layout type="log4net.Layout.XMLLayout" /> --> 

</appender> 



<!-- Setup the root category, add the appenders and set the default level --> 

<root> 

<level value="ALL" /> 

<appender-ref ref="LogFileAppender" /> 

<!-- <appender-ref ref="A" /> --> 

</root> 



<!-- Specify the level for some specific categories --> 

<logger name="SLog4net.Form1"> 

<!-- <appender-ref ref="B" /> --> 

<level value="ALL" /> 

<appender-ref ref="RollingLogFileAppender" /> 

</logger> 

</log4net> 

</configuration> 

//調用------------------------------------------------------------ 

using System; 

using System.Collections.Generic; 

using System.ComponentModel; 

using System.Data; 

using System.Drawing; 

using System.Text; 

using System.Windows.Forms; 

using log4net; 



[assembly: log4net.Config.DOMConfigurator(Watch = true)] 

namespace WindowsApplication1 

{ 





public partial class Form1 : Form 

{ 

public Form1() 

{ 

InitializeComponent(); 

} 





private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); 





private void button1_Click(object sender, EventArgs e) 

{ 

log.Warn("你好!"); 



} 

}

聯繫我們

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