ExceptionMsgManager.cs 2.1 KB
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OnlineStore.DeviceLibrary
{
    /// <summary>
    /// 异常信息管理
    /// </summary>
   public class ExceptionMsgManager
    {
        public static Dictionary<string, ExceptionMsg> msgMap = new Dictionary<string, ExceptionMsg>();

        public static void updateMsg(string module, string name, string msg = "")
        {
            try
            {
                string key = module + "-" + name;
                if (msgMap.ContainsKey(key))
                {
                    if (!string.IsNullOrEmpty(msg))
                    {
                        ExceptionMsg obj = new ExceptionMsg(module, name, msg);
                        msgMap[key] = obj;
                    }
                    else
                    {
                        msgMap.Remove(key);
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(msg))
                    {
                        ExceptionMsg obj = new ExceptionMsg(module, name, msg);
                        msgMap.Add(key, obj);
                    }
                }
            }
            catch (Exception ex)
            {
                LogUtil.error($"updateMsg[{module}][{name}][{msg}] error :" + ex.ToString());
            }
        }

        public static List<ExceptionMsg> getList()
        {
            return new List<ExceptionMsg>(msgMap.Values);
        }
    }

    public class ExceptionMsg
    {
        public ExceptionMsg()
        {
            updateTime = DateTime.Now;
        }

        public ExceptionMsg(string module,string name,string msg)
        {
            this.module = module;
            this.name = name;
            this.message = msg;
            updateTime = DateTime.Now;

        }
        public string module { get; set; }

        public string name { get; set; }

        public DateTime updateTime { get; set; }

        public string message { get; set; }
    }
}