LogUtil.cs 4.4 KB
using log4net;
using System;
using System.Reflection;
using System.Drawing;
using log4net.Repository;
using log4net.Config;
using System.IO;
using System.Text;
using System.Collections.Generic;

namespace DL.Utils
{
    public class LogUtil
    {
        static Dictionary<string, ILog> LogMap = new Dictionary<string, ILog>();
        public delegate void ShowMsg(string msg);
        public static event ShowMsg ShowDebug;
        public static event ShowMsg ShowInfo;
        public static event ShowMsg ShowWarn;
        public static event ShowMsg ShowError;
       static readonly ILog defaultLog = LogManager.GetLogger("standrobot");
        public static void Init()
        {
            XmlConfigurator.Configure(new FileInfo("log4net.config"));
            LogMap.Add("AIOBOX", defaultLog);
        }
        public static void AddLogMap(string key, ILog log)
        {
            LogMap.Add(key, log);
        }
        #region Main log
        public static void Debug(string msg, ILog log = null)
        {
            ShowDebug?.Invoke(msg);
            if (log == null)
                defaultLog.Debug(msg);
            else
                log.Debug(msg);
        }
        public static void Info(string msg, ILog log = null)
        {
            ShowInfo?.Invoke(msg);
            if (log == null)
                defaultLog.Info(msg);
            else
                log.Info(msg);
        }
        public static void Info(string msg, string logName)
        {
            ShowInfo?.Invoke(msg);
            if (string.IsNullOrEmpty(logName))
                defaultLog.Info(msg);
            else if (LogMap.ContainsKey(logName) && LogMap[logName] != null)
                LogMap[logName].Info(msg);
        }
        public static void Warn(string msg, ILog log = null)
        {
            ShowWarn?.Invoke(msg);
            if (log == null)
                defaultLog.Warn(msg);
            else
                log.Warn(msg);
        }
        public static void Error(string msg, Exception exception, ILog log)
        {
            StringBuilder sb = new StringBuilder(msg + "\r\n");
            if (log == null)
            {
                if (exception == null)
                    defaultLog.Error(msg);
                else
                {
                    defaultLog.Error(msg, exception);
                    sb.Append(exception.StackTrace);
                }
            }
            else
            {
                if (exception == null)
                    log.Error(msg);
                else
                {
                    log.Error(msg, exception);
                    sb.Append(exception.StackTrace);
                }
            }
            ShowError?.Invoke(sb.ToString());
        }
        public static void Error(string msg, ILog log, Exception exception)
        {
            StringBuilder sb = new StringBuilder(msg + "\r\n");
            if (log == null)
            {
                if (exception == null)
                    defaultLog.Error(msg);
                else
                {
                    defaultLog.Error(msg, exception);
                    sb.Append(exception.StackTrace);
                }
            }
            else
            {
                if (exception == null)
                    log.Error(msg);
                else
                {
                    log.Error(msg, exception);
                    sb.Append(exception.StackTrace);
                }
            }
            ShowError?.Invoke(sb.ToString());
        }
        public static void Error(string msg, ILog log)
        {
            StringBuilder sb = new StringBuilder(msg + "\r\n");
            if (log == null)
            {
                defaultLog.Error(msg);
            }
            else
            {
                log.Error(msg);
            }
            ShowError?.Invoke(sb.ToString());
        }
        public static void Error(string msg, Exception exception)
        {
            StringBuilder sb = new StringBuilder(msg + "\r\n");
            if (exception == null)
                defaultLog.Error(msg);
            else
            {
                defaultLog.Error(msg, exception);
                sb.Append(exception.StackTrace);
            }


            ShowError?.Invoke(sb.ToString());
        }
        public static void Error(string msg)
        {
            defaultLog.Error(msg);

            ShowError?.Invoke(msg);
        }
        #endregion
    }
}