LogUtil.cs 4.1 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using System.Reflection;
using System.Drawing;
using System.Runtime.ExceptionServices;
using System.Collections.Concurrent;
using System.Threading;

namespace OnlineStore.Common
{
    public class LogUtil
    {
        public delegate void ShowLogDelegate(string msg, Color color);
        public static event ShowLogDelegate ShowLog;
        public static readonly ILog LOGGER = LogManager.GetLogger("RollingLogFileAppender");


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

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

        public static void error(string errorMsg, int type, int seconds = 10)
        {
            try
            {
                if (lastErrorLogTime.ContainsKey(type))
                {
                    TimeSpan span = DateTime.Now - lastErrorLogTime[type];
                    if (span.TotalSeconds > seconds)
                    {
                        lastErrorLogTime[type] = DateTime.Now;
                        error(LOGGER, errorMsg);
                    }
                }
                else
                {
                    lastErrorLogTime.TryAdd(type, DateTime.Now);
                    error(LOGGER, errorMsg);
                }
            }
            catch (Exception ex)
            {
                LOGGER.Error(" 打印日志【" + type + "-" + errorMsg + "】出错:" + ex.ToString());
            }
        }
        public static void error(ILog log, string errorMsg, Exception ex = null)
        {
            if (errorMsg.Trim().Equals("") && (ex == null))
            {
                return;
            }
            if (ex == null)
            {
                log.Error(errorMsg);
            }
            else
            {
                log.Error(errorMsg, ex);
            }
            AddToBox(errorMsg, Color.Red);
        }
        private static object lockObj = "";
        private static void AddToBox(string msg, Color color)
        {
            if (Monitor.TryEnter(lockObj, 10))
            {
                try
                {
                    ShowLog?.Invoke(msg, color);
                }
                catch (Exception ex)
                {
                    LOGGER.Error("出错:", ex);
                }
                finally
                {
                    Monitor.Exit(lockObj);
                }
            }
            else
            {
                LOGGER.Debug("ShowLogPro【" + msg + "】失败,未得到锁");
            }
        }

        private static DateTime lastTime = DateTime.Now;

        public static void debug(string msg)
        {
            debug(LOGGER, msg);
        }
        public static void error(string errorMsg, Exception ex = null)
        {
            error(LOGGER, errorMsg, ex);
        }
        public static string lastlog = "";
        public static void info(string msg)
        {
            if (lastlog != msg)
            {
                info(LOGGER, msg);
                lastlog = msg;
            }
        }
        [System.Runtime.InteropServices.DllImport("kernel32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
        public static extern void OutputDebugString(string message);
    }
}