-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrorHandler.cs
More file actions
143 lines (111 loc) · 3.9 KB
/
Copy pathErrorHandler.cs
File metadata and controls
143 lines (111 loc) · 3.9 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
using System;
using System.Diagnostics;
using System.Reflection;
using System.Text;
namespace CoreObjects
{
public class ErrorHandler
{
protected string _desc = string.Empty;
#region [ Constructors ]
public ErrorHandler()
{ }
public ErrorHandler(Exception ex)
{
SetErrorInformation(ex);
}
public ErrorHandler(StringBuilder addlInformation, Exception ex)
{
SetErrorInformation(addlInformation, ex);
}
#endregion
#region [ Property Assignments ]
public string Description
{
get { return _desc; }
set { _desc = value; }
}
#endregion
public void SetErrorInformation(Exception ex)
{
StackTrace CallStack = new StackTrace(); // The call stack
StackFrame Frame = CallStack.GetFrame(1); // The frame that called me
MethodBase Method = Frame.GetMethod(); // The method that called me
SetErrorInformation(Method.DeclaringType.FullName + "." + Method.Name, "", ex);
Method = null;
Frame = null;
CallStack = null;
}
public void SetErrorInformation(StringBuilder addlInformation, Exception ex)
{
StackTrace CallStack = new StackTrace(); // The call stack
StackFrame Frame = CallStack.GetFrame(1); // The frame that called me
MethodBase Method = Frame.GetMethod(); // The method that called me
SetErrorInformation(Method.DeclaringType.FullName + "." + Method.Name, addlInformation.ToString(), ex);
Method = null;
Frame = null;
CallStack = null;
}
public void SetErrorInformation(string method, Exception ex)
{
SetErrorInformation(method, string.Empty, ex);
}
public void SetErrorInformation(string method, string addlInformation, Exception ex)
{
try
{
if (ex == null)
return;
if (ex.ToString().IndexOf("+++++++++ Start of Error +++++++++") > 0) // We don't want to keep logging every Catch!
{
_desc = ex.ToString().Substring(0, ex.ToString().IndexOf("+++++++++ End of Error +++++++++")) + "+++++++++ End of Error +++++++++" + Environment.NewLine + Environment.NewLine;
_desc = _desc.Replace("Exception:", string.Empty);
return;
}
if (ex.InnerException != null)
SetErrorInformation(string.Empty, string.Empty, ex.InnerException);
if (addlInformation.Length == 0)
addlInformation = string.Empty;
if (method.Length == 0)
method = string.Empty;
_desc = Environment.NewLine + Environment.NewLine + "+++++++++ Start of Error +++++++++" + Environment.NewLine + Environment.NewLine;
_desc += "*** Source ***" + Environment.NewLine + ex.Source + Environment.NewLine + Environment.NewLine;
if (method.Trim().Length > 0)
_desc += "*** Method ***" + Environment.NewLine + method + Environment.NewLine + Environment.NewLine;
if (addlInformation.Trim().Length > 0)
_desc += "*** Additional Information ***" + Environment.NewLine + addlInformation + Environment.NewLine + Environment.NewLine;
_desc += "*** Exception ***" + Environment.NewLine + ex.ToString() + Environment.NewLine + Environment.NewLine;
_desc += "*** Stack Trace ***" + Environment.NewLine + ex.StackTrace + Environment.NewLine + Environment.NewLine;
_desc += "+++++++++ End of Error +++++++++" + Environment.NewLine + Environment.NewLine;
}
catch
{
return;
}
}
public void SetErrorInformation(string method, StringBuilder addlInformation, Exception ex)
{
try
{
string addl = string.Empty;
if (ex == null)
return;
if (ex.InnerException != null)
SetErrorInformation(string.Empty, string.Empty, ex.InnerException);
if (addlInformation != null)
addl = addlInformation.ToString().Trim();
if (method == null)
method = string.Empty;
_desc += " at " + method + Environment.NewLine;
if (addl.Length > 0)
_desc += " (" + addl + ") " + Environment.NewLine;
_desc += ex.ToString() + Environment.NewLine;
_desc += " Stack Trace: " + ex.StackTrace + Environment.NewLine;
}
catch
{
return;
}
}
}
}