首页 > 代码库 > 在C#中使用LOG4NET(winform程序

在C#中使用LOG4NET(winform程序

http://www.csharpwin.com/csharpspace/678.shtml

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)] ) 

在类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>

  <configSections>

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

  </configSections>

  <appSettings>

  </appSettings>

  <log4net>

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

      <file value="http://www.mamicode.com/DMHJPGRKLog.log" />   文件名

      <appendToFile value="http://www.mamicode.com/true" />

      <rollingStyle value="http://www.mamicode.com/Size" />

      <maxSizeRollBackups value="http://www.mamicode.com/3" />

      <maximumFileSize value="http://www.mamicode.com/30MB" />

      <staticLogFileName value="http://www.mamicode.com/true" />

      <datePattern value="http://www.mamicode.com/yyyyMMdd-HH:mm:ss" />

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

        <conversionPattern value="http://www.mamicode.com/%date[TID:%-5thread][%level][%logger] %message%newline" />

      </layout>

    </appender>

    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">

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

        <conversionPattern value="http://www.mamicode.com/%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />

      </layout>

    </appender>

    <root>

      <level value="http://www.mamicode.com/ALL" />

      <appender-ref ref="LogFileAppender" />

      <appender-ref ref="ConsoleAppender" />

    </root>

  </log4net>

</configuration>

 

 

 

AssemblyInfo.cs

 

 

// 将log4net添加到本工程

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "RK.config", Watch = true)] 

 

 

 

 

 

 

 

//调用------------------------------------------------------------ 


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("你好!"); 



} 

}