• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
蓝色的大海
我很卑微.
博客园    首页    新随笔    联系   管理    订阅  订阅

实现通过Log4net监听并且广播信息

     Log4net是基于.net开发的一款非常著名的记录日志开源组件,用户要在自己的程序里加入日志功能,只需将log4net.dll引入工程即可。。

     下面主要讲述Log4net监听作用的实现,也就是说在程序中获得Log4net监听到的信息,并且通过委托的方式广播出去。

配置文件的写法

 <log4net>

     //下面是负责监听结点的配置
    <appender name="LogListener"

     //iView.Common 是命名空间,DebugLogListener类名,iViewCommon程序集     type="iView.Common.DebugLogListener,iViewCommon" >
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%d{HH:mm:ss.fff} %p %c.%M %L - %m%n" />
      </layout>

 

     //下面是负责写文件的结点
    </appender>

    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value=".\server_log.txt" />
      <param name="AppendToFile" value="true" />
      <param name="MaxSizeRollBackups" value="10" />
      <param name="MaximumFileSize" value="10MB" />
      <param name="RollingStyle" value="Size" />
      <param name="StaticLogFileName" value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss.fff} %p %c{1}.%M - %m%n" />
      </layout>
    </appender>

    <root>
      <level value="ALL" />
      <appender-ref ref="LogListener" />
      <appender-ref ref="RollingFileAppender" />
    </root>

  </log4net>

 

 

配置文件中DebugLogListener类的写法

 

 

using System;
using System.Collections.Generic;
using iView.Common;
using iView.Common.Forms;
using log4net.Appender;
using log4net.Core;
using log4net.Layout;

namespace iView.Common
{
   
/// <summary>
    /// Log4netListener
通过继承AppenderSkeleton实现IAppender接口,并能通过Log事件向外广播LoggingEvent消息
    /// </summary>
    public class DebugLogListener : AppenderSkeleton
    {
       
/// <summary>
        /// DebugEventArgs
的事件委托对象
        /// </summary>
        public static event EventHandler<DebugLogEventArgs> DebbugLog;

        /// <summary>
        ///
监听Log4net信息
        /// </summary>
        protected override void Append(LoggingEvent loggingEvent)
        {
            if (DebbugLog != null)
            {

               //loggingEvent事件中存在所有信息              

               //base.RenderLoggingEvent(loggingEvent)返回你所配置的信息字符串


                DebbugLog(this, new DebugLogEventArgs(base.RenderLoggingEvent(loggingEvent)));
            }
        }
    }
}

 

 

 事件的定义

 

 using System;
using System.Collections.Generic;
using System.Text;
using log4net.Core;

namespace iView.Common
{
   
/// <summary>
    /// DebugEventArgs
事件
    /// </summary>

    public class DebugLogEventArgs:EventArgs
    {
        /// <summary>
        ///
保存事件信息
        /// </summary>
        private string msg;
       
        /// <summary>
        ///
事件信息访问器
        /// </summary>
        public string Message
        {
            get { return msg; }
            set { msg = value; }
        }

        /// <summary>
        ///
构造函数
        /// </summary>
        /// <param name="messageData">
事件信息</param>
        public DebugLogEventArgs(string messageData)
        {
            msg = messageData;
        }      
    }

} 

 

显示信息处代码

 

 

 public partial class TestForm : Form  

  {
        public TestForm()
        {
            InitializeComponent();
        }

        private void TestForm_Load(object sender, EventArgs e)
        {
            DebugLogListener.DebbugLog += new EventHandler<DebugLogEventArgs>(Log4netListener_DebbugLog);
        }

        void Log4netListener_DebbugLog(object sender, DebugLogEventArgs e)
        {
            
        }

    }
}

 

 
 
 
  
 
posted @ 2008-09-21 13:17  blue th  阅读(529)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3