LogUtil.cs 6.2 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("RollingLogFileAppender");

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

        public static System.Windows.Forms.RichTextBox logBox = null;
        public static event ShowLog showLogEvent;
        public static int showCount = 30;

        public static bool debug_opened = false;


        public static void info(ILog log, string msg)
        {
            log.Info(" - " + msg);
            AddToBox(msg, Color.Black);
        }
        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>();
        public static void error(string errorMsg, int type, int spanSeconds = 10)
        {
            try
            {
                if (lastErrorLogTime.ContainsKey(type))
                {
                    TimeSpan span = DateTime.Now - lastErrorLogTime[type];
                    if (span.TotalSeconds < spanSeconds)
                    {
                        return;
                    }
                    else
                    {
                        lastErrorLogTime.Remove(type);
                        lastErrorLogTime.Add(type, DateTime.Now);
                        error(LOGGER, errorMsg);
                    }
                }
                else
                {
                    lastErrorLogTime.Add(type, DateTime.Now);
                    error(LOGGER, errorMsg);
                }
            }
            catch (Exception ex)
            {
                LOGGER.Error(" 打印日志【" + type + "-" + errorMsg + "】出错:" + ex.ToString());
            }
        }
        public static void error(ILog log, string errorMsg)
        {
            log.Error(" - " + errorMsg);
            AddToBox(errorMsg, 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 = string.Join("",logList);

                System.DateTime now = System.DateTime.Now;
                string newmsg = now.ToLongTimeString() + "  " + msg + Environment.NewLine;
                logList.Add(newmsg);

                showLogEvent?.Invoke(text+ newmsg, color);

                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 > 3000000)
                    {
                        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()
        {
            try
            {
                if (logBox != null && logBox.Visible)
                {
                    logBox.Text = LastText;

                    TimeSpan span = DateTime.Now - lastTime;
                    if (span.TotalSeconds > 3000000)
                    {
                        lastTime = DateTime.Now;
                        logBox.Select(logBox.Text.Length, 0);  //设置光标的位置到文本尾 
                        logBox.ScrollToCaret(); //滚动到控件光标处 
                    }
                }
            }
            catch (Exception ex)
            {
                LOGGER.Error("出错:" + ex.ToString());
            }
        }
        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)
        {
            if (ex == null)
            { 
                error(LOGGER, errorMsg);
            }
            else
            {
                error(LOGGER, errorMsg + ex.ToString());
            }
        }
        public static void info(string msg)
        {
            info(LOGGER, msg);
        }

    }
}