HDLogUtil.cs 3.5 KB
using IDHIKCamera;
using log4net;
using log4net.Config;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace CodeLibrary
{
    public class HDLogUtil
    {
        private static string logName = "";
        public static string LogName
        {
            get { return logName; }
            set
            {
                logName = value;
                LOGGER = LogManager.GetLogger(LogName);
            }
        }
        private static ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        private static Dictionary<int, DateTime> lastErrorLogTime = new Dictionary<int, DateTime>();
        public static System.Windows.Forms.RichTextBox logBox = null;
        public static int showCount = 50;
        public static bool debug_opened = false;
        static HDLogUtil()
        {
            XmlConfigurator.Configure();
            //LibLogUtil.LogEvent += LibLogUtil_LogEvent;
        }
        /*
        private static void LibLogUtil_LogEvent(LibLogEventArg libLogEventArg)
        {
            if (libLogEventArg.Level.Equals(LibLogLevel.Info))
            {

                HDLogUtil.info(libLogEventArg.Msg + " " + libLogEventArg.Exception?.ToString());
            }
            else if (libLogEventArg.Level.Equals(LibLogLevel.Warning))
            {

                HDLogUtil.info(libLogEventArg.Msg + " " + libLogEventArg.Exception?.ToString());
            }
            else if (libLogEventArg.Level.Equals(LibLogLevel.Debug))
            {

                HDLogUtil.debug(libLogEventArg.Msg + " " + libLogEventArg.Exception?.ToString());
            }
            else if (libLogEventArg.Level.Equals(LibLogLevel.Error))
            {

                HDLogUtil.error(libLogEventArg.Msg + " " + libLogEventArg.Exception?.ToString());
            }
        }
        */
        public static void info(string msg)
        {
            LOGGER.Info("HDLogUtil" + " - " + msg);
            AddToBox(msg);
        }
        public static void debug(string msg)
        {
            LOGGER.Debug("HDLogUtil" + " - " + msg);
            if (debug_opened)
            {
                AddToBox(msg);
            }
        }

        public static void error(string errorMsg)
        {
            LOGGER.Error("HDLogUtil" + " - " + errorMsg);
            AddToBox(errorMsg);
        }
        private static void AddToBox(string msg)
        {
            try
            {
                if (logBox == null)
                {
                    return;
                }
                ShowLogPro(msg);
            }
            catch (Exception ex)
            {
                LOGGER.Error("出错:" + ex.StackTrace);
            }
        }
        private static int count = 0;
        private static void ShowLogPro(string msg)
        {
            try
            {
                if (count > showCount)
                {
                    count = 0;
                    logBox.Clear();
                }
                System.DateTime now = System.DateTime.Now;
                logBox.AppendText(now.ToLongTimeString() + "  " + msg + Environment.NewLine);
                count++;
            }
            catch (Exception ex)
            {
                LOGGER.Error("出错:" + ex.ToString());
            }
        }

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