22
schangxiang@126.com
2024-11-28 aa1fa59aef9c9a115dee92e505752c9a8e7b7265
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using iWareCommon.Utils;
using iWareSql;
using iWareSql.Entity;
using iWareSql.Orm;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using iWareSql.DBModel;
 
namespace iWareModel
{
    /// <summary>
    /// 异常对象处理类
    /// </summary>
    public class SysExceptionInfoHandler
    {
 
        /// <summary>
        /// 初始化异常信息类(仅仅是获取异常信息对象)
        /// </summary>
        /// <typeparam name="T">入参类</typeparam>
        /// <param name="namespaceName">日志类型</param>
        /// <param name="namespaceName">当前命名空间名</param>
        /// <param name="exceptionFun">方法名</param>
        /// <param name="param">入参类</param>
        /// <param name="exceptionSource">异常方向,默认是WIP接收</param>
        /// <param name="msgSource">消息来源 </param>
        /// <returns></returns>
        public static Base_SysExceptionInfo GetExceptionInfo<T>(LogType _logType, string namespaceName, string exceptionFun, T param,
            ExceptionLevel exceptionLevel = ExceptionLevel.BusinessError,
        string key1 = "", string key2 = "",
            ExceptionSource exceptionSource = ExceptionSource.Receive
             , string msgSource = "")
        {
            Base_SysExceptionInfo exception = new Base_SysExceptionInfo()
            {
                Id = Guid.NewGuid().ToString(),
                module = _logType.ToString(),
                creator = "sys",
                key1 = key1,
                key2 = key2,
                exceptionLevel = Convert.ToInt32(exceptionLevel).ToString(),//异常级别:默认是业务错误
                exceptionFun = namespaceName + "." + exceptionFun,//异常方法名
                exceptionSource = Convert.ToInt32(exceptionSource).ToString(),//异常方向
                sourceData = JsonConvert.SerializeObject(param), //入参
            };
            return exception;
        }
        /// <summary>
        /// Error情况下的异常信息赋值
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="exception"></param>
        public static void GetExceptionInfoForError(string errMsg, Exception ex, ref Base_SysExceptionInfo exception)
        {
            exception.exceptionMsg = errMsg + ":" + ex.Message + "|错误发生位置:" + ex.StackTrace;//增加错误位置 【EditBy shaocx,2020-04-01】
            exception.exceptionLevel = Convert.ToInt32(ExceptionLevel.Error).ToString();
            exception.exceptionData = JsonConvert.SerializeObject(ex);
            //*/
        }
        /// <summary>
        /// 插入异常数据内容
        /// </summary>
        /// <param name="eie">异常对象</param>
        /// <param name="isWriteTextLog">是否需要写入文本日志</param>
        public static void InsertExceptionInfo(Base_SysExceptionInfo eie, bool isWriteTextLog = false)
        {
            Task.Run(() =>
                  {
                      try
                      {
                          eie.createTime = DateTime.Now;
                          eie.host = MachineHelper.GetHostName();
 
 
                          using (DbModel dbContext = new DbModel())
                          {
                              dbContext.Base_SysExceptionInfo.Add(eie);
                              dbContext.SaveChanges();
                          }
                          if (isWriteTextLog)
                          {
                              Log4NetHelper.WriteErrorLog((LogType)Enum.Parse(typeof(LogType), eie.module), "插入异常数据内容:" + JsonConvert.SerializeObject(eie));
                          }
 
                      }
                      catch (Exception ex)
                      {
                          Log4NetHelper.WriteErrorLog((LogType)Enum.Parse(typeof(LogType), eie.module), "插入异常数据内容出现异常:" + JsonConvert.SerializeObject(ex) + ",eie:" + JsonConvert.SerializeObject(eie));
                      }
                  });
        }
 
    }
}