LogUtil.cs 5.0 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)
        { 
            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)
        {
            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);
            }
        }
        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>();
        private static void ShowLogPro(string msg, Color color)
        {
            try
            {

                //if (logList.Count > 0)
                //{
                //    logList.RemoveAt(0);
                //}
                //if (logList.Count >= showCount)
                //{
                //    logList.RemoveAt(logList.Count - 1);
                //} 
                //System.DateTime now = System.DateTime.Now; 
                //logList.Insert(0, now.ToLongTimeString() + "  " + msg + Environment.NewLine);
                //logList.Insert(0, Environment.NewLine);


                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;
                }

                logBox.Text = text;

                //  logBox.Focus(); //使文本框获取焦点
                logBox.AppendText(now.ToLongTimeString() + "  " + msg + Environment.NewLine); //增加文本
                logBox.Select(logBox.Text.Length, 0);  //设置光标的位置到文本尾 
                logBox.ScrollToCaret(); //滚动到控件光标处 
            }
            catch (Exception ex)
            {
                LOGGER.Error("出错:" + ex.ToString());
            }
        }
        public static void ClearLog()
        {
            if (logBox != null)
            {
                logList.Clear();
                logBox.Text = "";
            }
        }

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

    }
}