熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

在C#中使用LOG4NET(winform程序)

2013-11-13 09:46:12  來源: .NET編程 

下載lognet (Google lognet)  
 
unzip lognet  
 
運行VS新建 c# Windows應用程序  
 
添加引用LogNET  
 
新建一個應用程序配置文件nfig(具體內容附在後面)  
 
打開Formcs  
 
  在Namespace上添加一行  [assembly: lognetConfigDOMConfigurator(Watch=true)]  
(或者 編輯Assemblycs文件添加如下內容:  
[assembly:lognetConfigDOMConfigurator( ConfigFileExtension=configWatch=true)] ) 
 
  在類Form中添加一個靜態變量  
 
                private static readonly lognetILog log = lognetLogManagerGetLogger(SystemReflectionMethodBaseGetCurrentMethod()DeclaringType);  
 
 
 
添加一個按鈕在按鈕處理函數中添加一行 logWarn(你好!);  
 
 
 
運行程序點一下按鈕  
 
 
 
OK打開Bin\Debug\logfiletxt可以看到你好  
 
 
 
 
 
附nfig  
 
 
 
<?xml version= encoding=utf ?>  
 
<configuration>  
 
<! Register a section handler for the lognet section >  
 
<configSections>  
 
  <section name=lognet type=SystemConfigurationIgnoreSectionHandler />  
 
</configSections>  
 
<appSettings>  
 
  <! To enable internal lognet logging specify the following appSettings key >  
 
  <! <add key=lognetInternalDebug value=true/> >  
 
</appSettings>  
 
<! This section contains the lognet configuration settings >  
 
<lognet>  
 
  <! Define some output appenders >  
 
  <appender name=LogFileAppender type=lognetAppenderFileAppender>  
 
    <param name=File value=logfiletxt />  
 
    <! Example using environment variables in params >  
 
    <! <param name=File value=${TMP}\\logfiletxt /> >  
 
    <param name=AppendToFile value=true />  
 
    <! An alternate output encoding can be specified >  
 
    <! <param name=Encoding value=unicodeFFFE /> >  
 
    <layout type=lognetLayoutPatternLayout>  
 
    <param name=Header value=[Header]\r\n />  
 
    <param name=Footer value=[Footer]\r\n />  
 
    <param name=ConversionPattern value=%d [%t] %p %c [%x] <%X{auth}> %m%n />  
 
    </layout>  
 
    <! Alternate layout using XML     
 
    <layout type=lognetLayoutXMLLayout /> >  
 
  </appender>  
 
 
 
  <! Setup the root category add the appenders and set the default level >  
 



  <root>  
 
    <level value=ALL />  
 
    <appenderref ref=LogFileAppender />  
 
    <! <appenderref ref=A /> >  
 
  </root>  
 
   
 
  <! Specify the level for some specific categories >  
 
  <logger name=SLognetForm>  
 
    <! <appenderref ref=B /> >  
 
    <level value=ALL />  
 
    <appenderref ref=RollingLogFileAppender />  
 
  </logger>  
 
</lognet>  
 
</configuration>  
 
 
 
 
 
nfig  
 
<?xml version= encoding=utf ?>  
 
<configuration>  
 
  <! Register a section handler for the lognet section >  
 
  <configSections>  
 
    <section name=lognet type=SystemConfigurationIgnoreSectionHandler />  
 
  </configSections>  
 
  <appSettings>  
 
    <! To enable internal lognet logging specify the following appSettings key >  
 
    <! <add key=lognetInternalDebug value=true/> >  
 
  </appSettings>  
 
  <! This section contains the lognet configuration settings >  
 
  <lognet>  
 
    <! Define some output appenders >  
 
    <appender name=LogFileAppender type=lognetAppenderFileAppender>  
 
      <param name=File value=logfiletxt />  
 
      <! Example using environment variables in params >  
 
      <! <param name=File value=${TMP}\\logfiletxt /> >  
 
      <param name=AppendToFile value=true />  
 
      <! An alternate output encoding can be specified >  
 
      <! <param name=Encoding value=unicodeFFFE /> >  
 
      <layout type=lognetLayoutPatternLayout>  
 
        <param name=Header value=[Header]\r\n />  
 
        <param name=Footer value=[Footer]\r\n />  
 
        <param name=ConversionPattern value=%d [%t] %p %c [%x] <%X{auth}> %m%n />  
 
      </layout>  
 
      <! Alternate layout using XML     
 
    <layout type=lognetLayoutXMLLayout /> >  
 
    </appender>  
  
    <! Setup the root category add the appenders and set the default level >  
 
    <root>  
 
      <level value=ALL />  
 
      <appenderref ref=LogFileAppender />  
 
      <! <appenderref ref=A /> >  
 
    </root>  
 
 
 
    <! Specify the level for some specific categories >  
 
    <logger name=SLognetForm>  
 
      <! <appenderref ref=B /> >  
 
      <level value=ALL />  
 
      <appenderref ref=RollingLogFileAppender />  
 
    </logger>  
 
  </lognet>  
 
</configuration>  
 
//調用  
 
using System;  
 
using SystemCollectionsGeneric;  
 
using SystemComponentModel;  
 
using SystemData;  
 
using SystemDrawing;  
 
using SystemText;  
 
using SystemWindowsForms;  
 
using lognet;  
 
 
 
[assembly: lognetConfigDOMConfigurator(Watch = true)]  
 
namespace WindowsApplication  
 
{  
  
    public partial class Form : Form  
 
    {  
 
        public Form()  
 
        {  
 
            InitializeComponent();  
 
        }  
 
        private static readonly lognetILog log = lognetLogManagerGetLogger(SystemReflectionMethodBaseGetCurrentMethod()DeclaringType);  
  
        private void button_Click(object sender EventArgs e)  
 
        {  
 
            logWarn(你好!);  
 
 
 
        }  
 
    }


From:http://tw.wingwit.com/Article/program/net/201311/11537.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.