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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值