LogUtil.cs 3.2 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;
using log4net.Util;

namespace AGVLib
{
    internal class LogUtil
    {
        static Dictionary<string, LogBean> LogMap = new Dictionary<string, LogBean>();
        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 LogBean defaultLog = new LogBean("AppLog");
        static LogBean getLogBean(string logname)
        {
            if (LogMap.ContainsKey(logname))
                return LogMap[logname];
            return null;
        }
        public static void Init()
        {
            XmlConfigurator.Configure(new FileInfo("log4net.config"));
        }
        public static void AddLogMap(string key, LogBean log)
        {
            if (!LogMap.ContainsKey(key))
                LogMap.Add(key, log);
        }
        #region Main log
        public static void Debug(string msg, LogBean log = null)
        {
            ShowDebug?.Invoke(msg);
            if (log == null)
                defaultLog.Debug(msg);
            else
                log.Debug(msg);
        }
        public static void Info(string msg, LogBean log = null)
        {
            ShowInfo?.Invoke(msg);
            if (log == null)
                defaultLog.Info(msg);
            else
                log.Info(msg);
        }
        public static void Info(string msg, string logName)
        {
            LogBean log = getLogBean(logName);
            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, LogBean log = null)
        {
            ShowWarn?.Invoke(msg);
            if (log == null)
                defaultLog.Warn(msg);
            else
                log.Warn(msg);
        }
        public static void Error(string msg, Exception exception = null, LogBean log = null)
        {
            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());
        }
        #endregion
    }
}