log4net在winforn中的使用

1、新建一个面目

2、添加引用(添加log4net.dll的引用)

3、新建一个App.config文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" 
    type="log4net.Config.Log4NetConfigurationSectionHandler,log4net-net-1.2.10.0" 
   />
  </configSections>


  <log4net>
    <root>
      <level value="ALL" />
      <appender-ref ref="LogFileAppender" />
    </root>

    <appender name="LogFileAppender" type="log4net.Appender.FileAppender" >
      <param name="File" value="log-file.txt" />
      <param name="AppendToFile" value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] &lt;%X{auth}&gt;%n - %m%n" />
      </layout>
    </appender>
  </log4net>
</configuration>

4、第一种方法:在Properties目录下 的AssemblyInfo.cs文件添加一句代码(在命名空间下添加

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

    第二种方法:在每个要记录日志的form中添加[assembly: log4net.Config.DOMConfigurator(Watch = true)](在命名空间下添加

 5、应用

 

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 Log
{
    public partial class Form1 : Form
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);


        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo("log4net.config"));

            //log4net.ILog log = log4net.LogManager.GetLogger(typeof(Form1));
            //log.Debug("hello");
            log.Warn("你好!");

        }
    }
}

 

你可能感兴趣的:(C++,c,.net,xml,windows)