ExceptionsHelp.cs
7.29 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using Newtonsoft.Json;
namespace Hh.Mes.Common.log
{
/// <summary>
/// 异常类
/// </summary>
public class ExceptionsHelp
{
private static volatile ExceptionsHelp _process = null;
private static readonly object LockHelper = new object();
public readonly string ReturnMsg500 = "{\"Code\":500,\"Status\":fasle,\"Message\":\"api 500 error\",\"Result\":null}";
public readonly string ReturnMsgList500 = "{\"code\":-1,\"count\":0,\"msg\":\"api 500 error\",\"data\":null}";
private ExceptionsHelp()
{
}
/// <summary> Gets the instance
/// </summary>
public static ExceptionsHelp Instance
{
get
{
if (_process == null)
{
lock (LockHelper)
{
if (_process == null)
{
_process = new ExceptionsHelp();
}
}
}
return _process;
}
}
/// <summary>异常日志
/// </summary>
/// <param name="action">action</param>
/// <param name="methodName">methodName</param>
/// <param name="actionFinally">异常finally 执行动作(用于销毁、释放对象等)</param>
public void ExecuteVoidFunc(Action action, string methodName = "", Action actionFinally = null, string catchRetrunValue = "action")
{
if (action == null) return;
string tempMethodName = (methodName + "").Length == 0 ? action.Method.Name : methodName;
try
{
action.Invoke();
}
catch (System.Threading.ThreadAbortException)
{
//不作处理 (合同打印触发异常)
}
catch (AggregateException ex)
{
Log4NetHelper.Instance.Error($"【{tempMethodName}】" + ex.Message);
//首先任务是并行计算的,处理过程中可能会产生n多的异常,普通的Exception并不能获取到异常,然而为并行诞生的AggregateExcepation就可以获取到一组异常。
foreach (var single in ex.InnerExceptions)
{
}
}
catch (Exception ex)
{
Log4NetHelper.Instance.Error($"【{tempMethodName}】" + ex.Message);
throw new Exception(ex.Message);
}
finally
{
actionFinally?.Invoke();
}
}
public string ExecuteString(Func<string> action, string catchRetrunValue = "action")
{
try
{
return action.Invoke();
}
catch (Exception ex)
{
Log4NetHelper.Instance.Error(ex.Message);
return ReturnValue(ex, catchRetrunValue);
}
}
/// <summary>异常返回 ReturnMsg500
/// </summary>
/// <typeparam name="T">类型T</typeparam>
/// <param name="action">action</param>
/// <param name="methodName">methodName</param>
/// <param name="actionFinally"></param>
/// <param name="actionCatCh"></param>
/// <param name="catchRetrunValue">action 新增编辑删除, list 是layui table 返回格式</param>
/// <returns></returns>
public dynamic ExecuteT<T>(Func<T> action, string methodName = "", Action actionFinally = null,Action actionCatCh=null, string catchRetrunValue = "action")
{
//var s = Activator.CreateInstance<T>();
//var defaultType = default(T);
string tempMethodName = (methodName + "").Length == 0 ? action.Method.Name : methodName;
if (action == null) return ReturnMsg500;
try
{
return action.Invoke();
}
catch (ThreadAbortException ex)
{
actionCatCh?.Invoke();
//不作处理
return ReturnValue(ex, catchRetrunValue);
}
catch (AggregateException ex)
{
actionCatCh?.Invoke();
//首先任务是并行计算的,处理过程中可能会产生n多的异常,普通的Exception并不能获取到异常,然而为并行诞生的AggregateExcepation就可以获取到一组异常。
foreach (var single in ex.InnerExceptions)
{
Log4NetHelper.Instance.Error($"【{tempMethodName}】" + single.Message);
}
return ReturnValue(ex, catchRetrunValue);
}
catch (Exception ex)
{
actionCatCh?.Invoke();
Log4NetHelper.Instance.Error($"【{tempMethodName}】" + ex.Message);
return ReturnValue(ex, catchRetrunValue);
}
finally
{
actionFinally?.Invoke();
}
}
/// <summary>异常返回 T
/// </summary>
/// <typeparam name="T">类型T</typeparam>
/// <param name="action">action</param>
/// <param name="methodName">methodName</param>
/// <param name="actionFinally"></param>
/// <returns></returns>
public T Execute<T>(Func<T> action, string methodName = "", Action actionFinally = null)
{
var defaultType = default(T);
string tempMethodName = (methodName + "").Length == 0 ? action.Method.Name : methodName;
if (action == null) return defaultType;
try
{
return action.Invoke();
}
catch (System.Threading.ThreadAbortException)
{
//不作处理
return defaultType;
}
catch (AggregateException ex)
{
//首先任务是并行计算的,处理过程中可能会产生n多的异常,普通的Exception并不能获取到异常,然而为并行诞生的AggregateExcepation就可以获取到一组异常。
foreach (var single in ex.InnerExceptions)
{
Log4NetHelper.Instance.Error($"【{tempMethodName}】" + single.Message);
}
//日志
return defaultType;
}
catch (Exception ex)
{
Log4NetHelper.Instance.Error($"【{tempMethodName}】" + ex.Message);
//日志
return defaultType;
}
finally
{
actionFinally?.Invoke();
}
}
/// <summary>
/// 异常返回信息 action 是普通操作,list 是layui table 返回格式
/// </summary>
private string ReturnValue(Exception ex, string catchRetrunValue)
{
var tempMes = ex.ToString().StrJsonReplace();
tempMes = tempMes.Substring(1, 160);
return catchRetrunValue == "action" ? "{\"Code\":500,\"Status\":false,\"Message\":\"" + tempMes + "\",\"Result\":null}"
: "{\"code\":-1,\"count\":0,\"msg\":\"" + tempMes + "\",\"data\":null}";
}
}
}