using System;
using System.Collections.Generic;
using System.Text;
using log4net;
using log4net.Appender;
using log4net.Core;
using System.Diagnostics;
using log4net.Layout;

namespace ChuPiao.Common.Core.Utils
{
    /// <summary>
    /// 自定义的log4net Appender，其作用是在有log写入的时候引发事件，以便系统其它部分响应
    /// </summary>
    public class LogEventAppender : AppenderSkeleton
    {
        protected override void Append(log4net.Core.LoggingEvent loggingEvent)
        {
            if (null != OnLogging)
            {
                OnLogging(this, new LogEventArgs(loggingEvent, this.RenderLoggingEvent(loggingEvent)));
            }
        }
        public static event EventHandler<LogEventArgs> OnLogging;
    }

    public class LogEventArgs : EventArgs
    {
        public LogEventArgs(LoggingEvent loggingEvent, string renderedLoggingEvent)
        {
            LoggingEvent = loggingEvent;
            RenderedLoggingEvent = renderedLoggingEvent;
        }

        private string _renderedLoggingEvent;
        public string RenderedLoggingEvent
        {
            get { return _renderedLoggingEvent; }
            set { _renderedLoggingEvent = value; }
        }

        private LoggingEvent _loggingEvent;
        public LoggingEvent LoggingEvent
        {
            get { return _loggingEvent; }
            set { _loggingEvent = value; }
        }
    }
}
