LogUtil.cs 4.6 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
    {
        private static LogUtil instance = new LogUtil();
        public   delegate void ShowLog(string msg, Color color);
        public static readonly ILog LOGGER = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        public static readonly ILog AIOLog = LogManager.GetLogger("AIOBOXLog");
        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); 
            if (logBox == null)
            {
                return;
            } 
            AddToBox(msg, Color.Black); 
            //clear();
        }
        public static void info(ILog log,string msg, Color color)
        {
            log.Info( " - " + msg); 
            if (logBox == null)
            {
                return;
            }
            AddToBox(msg, color); 
        }
        public static void debug(ILog log, string msg, Color color)
        {
            log.Debug( " - " + msg);
            if (debug_opened)
            {
                if (logBox == null)
                {
                    return;
                }
                AddToBox(msg, color);
            }
        }
        public static void debug(ILog log, string msg)
        {
            log.Debug( " - " + msg);
            if (debug_opened)
            {
                if (logBox == null)
                {
                    return;
                }
                AddToBox(msg, Color.Gray); 
            }
        } 
        public static void error( string errorMsg,int type)
        {
            if (lastErrorLogTime.ContainsKey(type))
            {
                TimeSpan span = DateTime.Now - lastErrorLogTime[type];
                if (span.TotalSeconds < 10)
                {
                    return;
                }
                else
                {
                    lastErrorLogTime.Remove(type);
                    lastErrorLogTime.Add(type, DateTime.Now);
                    error( errorMsg);
                }
            }
            else
            {
                lastErrorLogTime.Add(type, DateTime.Now);
                error( errorMsg);
            }
        }
        public static void error(ILog log, string errorMsg)
        {
            //if (!lasErrorLogList.Contains(errorMsg))
            {
                log.Error( " - " + errorMsg);
                if (logBox == null)
                {
                    return;
                }
                AddToBox(errorMsg, Color.Red);
            } 
            //lasErrorLogList.Add(errorMsg);
            //if (lasErrorLogList.Count > errCount)
            //{
            //    lasErrorLogList.RemoveAt(0);
            //}
        }
        private static void AddToBox(string msg, Color color)
        {
            try
            { 
                ShowLogPro(msg); 
            }
            catch (Exception ex)
            {
                LOGGER.Error("出错:" + ex.StackTrace);
            } 
        }
        private static int count = 0;
        private static void ShowLogPro(string msg )
        {
            try
            {
                //clear();
                if (count > showCount)
                {
                    count = 0;
                    logBox.Clear();
                }
                //logBox.SelectionColor = color;
                System.DateTime now = System.DateTime.Now;
                logBox.AppendText(now.ToLongTimeString() + "  " + msg + Environment.NewLine);
                count++; 
            
            }
            catch (Exception ex)
            {
                LOGGER.Error("出错:" + ex.ToString());
            }
        }
        public static void debug(string msg)
        {
            debug(LOGGER, msg);
        }
        public static void error(string errorMsg)
        {
            error( LOGGER,errorMsg);
        }

        public static void ClearLog()
        {
            if (logBox != null)
            {
                logBox.Text = "";
                count = 0;
            }
        }

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