LogUtil.cs 5.7 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using System.Reflection;
using System.Drawing;


namespace OnlineStore.Common
{
    public class LogUtil
    {
        public static readonly ILog AIOLog = LogManager.GetLogger("AIOBOXLog");
        private static LogUtil instance = new LogUtil();
        public   delegate void ShowLog(string msg, Color color);
        public static readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        public static Dictionary<int, DateTime> lastErrorLogTime = new Dictionary<int, DateTime>();

        public static System.Windows.Forms.RichTextBox logBox = null;

        public static int showCount = 20;

        public static bool debug_opened = false;

       
        public static void info(ILog log,string msg )
        {
            if (log == null)
            {
                return;
            }

            log.Info( " - " + msg); 
          
            AddToBox(msg, Color.Black); 
            //clear();
        }
        public static void info(ILog log,string msg, Color color)
        {
            log.Info( " - " + msg); 
            
            AddToBox(msg, color); 
        }
        public static void debug(ILog log, string msg, Color color)
        {
            log.Debug( " - " + msg);
            if (debug_opened)
            { 
                AddToBox(msg, color);
            }
        }
        public static void debug(ILog log, string msg)
        {
            log.Debug( " - " + msg);
            if (debug_opened)
            { 
                AddToBox(msg, Color.Gray); 
            }
        }
     
        private static List<string> lasErrorLogList = new List<string>();
        private static int errCount = 5;

        public static void error(  string errorMsg,int type,int spanSeconds=10)
        {
            if (lastErrorLogTime.ContainsKey(type))
            {
                try
                {
                    TimeSpan span = DateTime.Now - lastErrorLogTime[type];
                    if (span.TotalSeconds < spanSeconds)
                    {
                        return;
                    }
                    else
                    {
                        lastErrorLogTime.Remove(type);
                        lastErrorLogTime.Add(type, DateTime.Now);
                        error( errorMsg);
                    }
                }catch(Exception ex)
                {

                }
            }
            else
            {
                lastErrorLogTime.Add(type, DateTime.Now);
                error( errorMsg);
            }
        }
        public static void error(ILog log, string errorMsg, Exception ex = null)
        {
            if (ex == null)
            {
                log.Error(errorMsg);
                AddToBox(errorMsg, Color.Red);
            }
            else
            {
                log.Error(errorMsg, ex);
                AddToBox(errorMsg + ex.ToString(), Color.Red);
            }
        }
        private static void AddToBox(string msg, Color color)
        {
            try
            { 
                ShowLogPro(msg, color); 
            }
            catch (Exception ex)
            {
                LOGGER.Error("出错:" + ex.StackTrace);
            } 
        }
        private static List<string> logList = new List<string>();
        public static string LastText = "";
        private static void ShowLogPro(string msg, Color color)
        {
            try
            {
                if (logList.Count > 0)
                {
                    // logList.RemoveAt(0);
                }
                if (logList.Count >= showCount)
                {
                    logList.RemoveAt(0);
                }
                string text = "";
                foreach (string str in logList)
                {
                    text += str;
                }
                System.DateTime now = System.DateTime.Now;
                logList.Add(now.ToLongTimeString() + "  " + msg + Environment.NewLine);
                if (logBox == null)
                {
                    return;
                }
                LastText = text;
                if (logBox.Visible)
                {
                    logBox.Text = text;

                    //  logBox.Focus(); //使文本框获取焦点
                    logBox.AppendText(now.ToLongTimeString() + "  " + msg + Environment.NewLine); //增加文本

                    TimeSpan span = DateTime.Now - lastTime;
                    if (span.TotalSeconds > 1000)
                    {
                        lastTime = DateTime.Now;
                        logBox.Select(logBox.Text.Length, 0);  //设置光标的位置到文本尾 
                        logBox.ScrollToCaret(); //滚动到控件光标处 
                    }
                }
            }
            catch (Exception ex)
            {
                LOGGER.Error("出错:" + ex.ToString());
            }
        }
        private static DateTime lastTime = DateTime.Now;
        public static void UpdateLogbox()
        {
            if (logBox != null && logBox.Visible)
            {
                logBox.Text = LastText; 
            }
        }
        public static void ClearLog()
        {

            if (logBox != null)
            {
                LastText = "";
                logList.Clear();
                logBox.Text = "";
            }
        }
        public static void debug(string msg)
        {
            debug(LOGGER, msg);
        }
        public static void error(string errorMsg,Exception ex=null)
        {
            error( LOGGER,errorMsg);
        }

      

        public static void info(string msg)
        {
            info(LOGGER,msg );
        } 
        
    }
}