zoukankan      html  css  js  c++  java
  • Log4Net: TextBoxBaseAppender

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using log4net.Appender;
    using System.Windows.Forms;
    using log4net.Core;
    using log4net.Layout;

    namespace UI
    {
        /// <summary>
        
    /// Usage:
        
    ///     log4net.Config.BasicConfigurator.Configure();
        
    ///     var logPattern = "%date [%thread] %-5level %logger !%M - %message%newline";
        
    ///     var logAppender = new TextBoxBaseAppender()
        
    ///     {
        
    ///         TextBox = this.textBox2,
        
    ///         Layout = new PatternLayout(logPattern)
        
    ///     };
        
    ///     
        
    ///     ((log4net.Repository.Hierarchy.Hierarchy)log4net.LogManager.GetLoggerRepository()).Root.AddAppender(logAppender);
        
    /// </summary>
        public class TextBoxBaseAppender : AppenderSkeleton
        {
            public TextBoxBase TextBox { getset; }

            public TextBoxBaseAppender()
            {
            }

            protected override void Append(LoggingEvent loggingEvent)
            {
                if (this.TextBox == null)
                {
                    return;
                }

                if(!this.TextBox.IsHandleCreated)
                {
                    return;
                }

                if(this.TextBox.IsDisposed)
                {
                    return;
                }

                var patternLayout = this.Layout as PatternLayout;

                var str = string.Empty;
                if (patternLayout != null)
                {
                    str = patternLayout.Format(loggingEvent);

                    if (loggingEvent.ExceptionObject != null)
                    {
                        str += loggingEvent.ExceptionObject.ToString() + Environment.NewLine;
                    }
                }
                else
                {
                    str = loggingEvent.LoggerName + "-" + loggingEvent.RenderedMessage + Environment.NewLine;
                }

                if (!this.TextBox.InvokeRequired)
                {
                    this.TextBox.AppendText(str);
                }
                else
                {
                    this.TextBox.BeginInvoke((MethodInvoker)delegate
                    {
                        if (!this.TextBox.IsHandleCreated)
                        {
                            return;
                        }

                        if (this.TextBox.IsDisposed)
                        {
                            return;
                        }
                        this.TextBox.AppendText(str);
                    });
                }
            }
        }
    }
  • 相关阅读:
    STM8S TIM4库函数应用
    几种更新(Update语句)查询的方法
    CentOS 配置httpd使局域网能够正常訪问
    天将降大任于斯人也,必先苦其心志,劳其筋骨,饿其体肤,空乏其身,行拂乱其所为,所以动心忍性,增益其所不能
    Numeral.js 是一个用于格式化和数字四则运算的js 库
    SVN高速新手教程
    我的Android开发相关文章
    cocos2d-x游戏开发实战原创视频讲座系列1之2048游戏开发
    运动检测(前景检测)之(一)ViBe
    linux概念之分区与文件系统
  • 原文地址:https://www.cnblogs.com/mrfangzheng/p/2795133.html
Copyright © 2011-2022 走看看