log4net 使用例子

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace TestLog4net
{
    class Program
    {
        DebugTextWriter debugTestWriter = null;
        static void Main(string[] args)
        {
            #region 方法1 直接配置
            //log4net.Layout.PatternLayout layout = new log4net.Layout.PatternLayout("[%d] [%-4thread] [%-5level] [%F] [%-20C{1}] - %message %newline");
            //log4net.Appender.DebugAppender appender = new log4net.Appender.DebugAppender();
            ////此为文本输出 testfile.xml位置在 ..\bin\Debug目录下
            ////log4net.Appender.FileAppender fappender = new log4net.Appender.FileAppender(layout, "testfile.xml");
            //appender.Layout = layout;
            //log4net.Config.BasicConfigurator.Configure(appender);
            ////log4net.Config.BasicConfigurator.Configure(fappender);
            #endregion

            #region 方法2 通过系统配置文件配置( App.congig + AssemblyInfo.cs)
            log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(
                @".\app.config"));
            #endregion
 
            LogMgr.Logger.Debug("开始");
            Console.WriteLine("1");
            LogMgr.Logger.Debug("writeline 1");
           

            Console.ReadLine();

        }

        public void LoadLog()
        {
            debugTestWriter = new DebugTextWriter();
            debugTestWriter.ValueWrited += new EventHandler<WriteEventArgs>(debugTestWriter_ValueWrited);
        }

        void debugTestWriter_ValueWrited(object sender, WriteEventArgs e)
        {
            Console.WriteLine(e.Value);
        }
    }

    class WriteEventArgs : EventArgs
    {
        string _value;
        public string Value
        {
            get { return _value; }
            set { _value = value; }
        }

        public WriteEventArgs(string value)
        {
            this.Value = value;
        }
    }

    class DebugTextWriter : System.IO.TextWriter
    {
        public event EventHandler<WriteEventArgs> ValueWrited;

        public DebugTextWriter()
        {

        }

        public override Encoding Encoding
        {
            get { return Encoding.UTF8; }
        }


        public override void Write(char[] buffer, int index, int count)
        {
            //base.Write(buffer, index, count);
            string log = new string(buffer, index, count);
            Write(log);
        }

        public override void Write(string value)
        {
            //base.Write(value);
            OnValueWrited(new WriteEventArgs(value));
        }

        protected virtual void OnValueWrited(WriteEventArgs e)
        {
            EventHandler<WriteEventArgs> handler = this.ValueWrited;
            if (handler != null)
            {
                handler(this, e);
            }
        }
    }

    class LogMgr
    {
        const string LOGGER_NAME = "app";
        static log4net.ILog _logger = log4net.LogManager.GetLogger(LOGGER_NAME);

        public static log4net.ILog Logger
        {
            get { return _logger; }
        }
    }
}

 AssemblyInfo.cs中加入:

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

app.config:

<?xml version="1.0" encoding="utf-8"?>
<!--
  .NET application configuration file    
  This file must have the exact same name as your application with .config appended to it.
  
  For example if your application is ConsoleApp.exe then the config file must be ConsoleApp.exe.config.
  It must also be in the same directory as the application.
 -->
<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>
    <appender name="DebugAppender" type="log4net.Appender.DebugAppender">
      <layout type="log4net.Layout.PatternLayout" value="[%d{mm:ss,fff}] [%-4thread] %-5level [%-10C{1}]- %message %newline" />
    </appender>

    <appender name ="LogFileAppender" type="log4net.Appender.FileAppender">
      <param name="File" value="test1.txt"/>
      <param name="AppendToFile" value="true" />
      <layout type="log4net.Layout.PatternLayout" value="[%d{mm:ss,fff}] [%-4thread] %-5level [%-10C{1}]- %message %newline" />
    </appender>
    <!-- Setup the root category, add the appenders and set the default level -->
    <root>
      <level value="ALL" />
      <appender-ref ref="DebugAppender" />
      <appender-ref ref="LogFileAppender" />

    </root>
  </log4net>
</configuration>

posted on 2010-09-06 13:51  源远流长  阅读(461)  评论(0编辑  收藏  举报

导航