v7.3.2018.0614   重构高性能资源池,减少GC压力,增加线程池,让异步任务得到平等竞争CPU的机会
大石头 authored at 2018-06-14 17:56:44
1.68 KiB
X
using System;
using System.Windows.Forms;

namespace NewLife.Log
{
    /// <summary>文本控件输出日志</summary>
    public class TextControlLog : Logger
    {
        private Control _Control;
        /// <summary>文本控件</summary>
        public Control Control { get { return _Control; } set { _Control = value; } }

        private Int32 _MaxLines = 1000;
        /// <summary>最大行数,超过该行数讲清空文本控件。默认1000行</summary>
        public Int32 MaxLines { get { return _MaxLines; } set { _MaxLines = value; } }

        /// <summary>写日志</summary>
        /// <param name="level"></param>
        /// <param name="format"></param>
        /// <param name="args"></param>
        protected override void OnWrite(LogLevel level, String format, params Object[] args)
        {
            WriteLog(Control, Format(format, args) + Environment.NewLine, MaxLines);
        }

#if !__MOBILE__
        /// <summary>在WinForm控件上输出日志,主要考虑非UI线程操作</summary>
        /// <remarks>不是常用功能,为了避免干扰常用功能,保持UseWinForm开头</remarks>
        /// <param name="control">要绑定日志输出的WinForm控件</param>
        /// <param name="msg">日志</param>
        /// <param name="maxLines">最大行数</param>
        public static void WriteLog(Control control, String msg, Int32 maxLines = 1000)
        {
            if (control == null) return;

            var txt = control as TextBoxBase;
            if (txt == null) throw new XException("不支持的控件类型{0}!", control.GetType());

            txt.Append(msg, maxLines);
        }
#endif
    }
}