-
Notifications
You must be signed in to change notification settings - Fork 0
/
DebugLogger.cs
280 lines (251 loc) · 8.93 KB
/
DebugLogger.cs
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Part of the LogNet logging library.
// Copyright (c) 2011 Nigel Hawkins
//
// Licensed under the Gnu LGPL version 3.
// See http://www.gnu.org/licenses/lgpl.html
using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Diagnostics;
using System.Reflection;
namespace LogNet
{
/// <remarks>
/// A static class which can be used to log debug information.
/// </remarks>
public static class DebugLogger
{
#region Variables and enumerations
private static String appName = "MyApp";
private static String fileName = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\MyApp.log";
private static LoggingLevels threshold = LoggingLevels.Critical;
private static bool enabled = true;
private static StringBuilder buffer = new StringBuilder();
#endregion
#region Public Properties
/// <summary>
/// Sets or returns the name of the application that we are logging for.
/// Errors will be logged in "My Documents\(AppName).log".
/// </summary>
public static String AppName {
get {
return appName;
}
set {
if (value.IndexOfAny("\\.".ToCharArray()) >= 0)
throw new ArgumentException(ResStrings.GetString("InvalidAppNameCharacters"));
appName = value;
fileName = Environment.GetFolderPath(Environment.SpecialFolder.Personal) +
"\\" + appName + ".log";
buffer.Length = 0;
buffer.Append("\r\n");
buffer.Append("------------------------------------------------------------\r\n");
buffer.Append("\r\n");
}
}
/// <summary>
/// Get or set the threshold logging level. Note that we will log all events at this level or
/// higher. This defaults to "Critical".
/// </summary>
public static LoggingLevels Threshold {
get { return threshold; }
set { threshold = value; }
}
/// <summary>
/// Get or set whether logging is enabled. This defaults to "True".
/// </summary>
public static bool Enabled {
get { return enabled; }
set { enabled = value; }
}
#endregion
#region Public Methods
/// <summary>
/// This static method logs a single action to the output file. This will
/// appear in the file looking like:
/// <code>2006-12-31 12:34:56 : The debug message.</code>
/// </summary>
/// <param name="source">The source object or method.</param>
/// <param name="what">The debug message we wish to record.</param>
/// <param name="priority">What priority should this be recorded at.</param>
public static void LogAction(string source,
string what,
LoggingLevels priority) {
if (!enabled) return;
if (priority < threshold) return;
buffer.AppendLine(GetReport(source, what, ""));
WriteBufferToFile();
}
/// <summary>
/// This static method logs a single action to the output file. This will
/// appear in the file looking like:
/// <code>2006-12-31 12:34:56 : The debug message.</code>
/// </summary>
/// <param name="source">The source object or method.</param>
/// <param name="what">The debug message we wish to record.</param>
/// <param name="data">Additional data to serialise and record.</param>
/// <param name="priority">What priority should this be recorded at.</param>
public static void LogAction(string source,
string what,
object data,
LoggingLevels priority) {
if (!enabled) return;
if (priority < threshold) return;
buffer.AppendLine(GetReport(source, what, Formatters.ASCIIfy(data)));
WriteBufferToFile();
}
/// <summary>
/// Log the current call stack to the log file.
/// </summary>
public static void LogCallStack(string info) {
LogThreadState(Thread.CurrentThread, info);
}
/// <summary>
/// Log a summary of the state of the current System.Threading.Thread.
/// </summary>
/// <param name="info">Informational string to be pre-pended to output.</param>
public static void LogThreadState(string info) {
LogThreadState(Thread.CurrentThread, info);
}
/// <summary>
/// Log a summary of the state of the specified System.Threading.Thread.
/// </summary>
/// <param name="t">The thread to log.</param>
/// <param name="info">Informational string to be pre-pended to output.</param>
public static void LogThreadState(Thread t,
string info) {
if (!enabled || t == null)
return;
StringBuilder sb = new StringBuilder();
sb.AppendLine(GetReport(t, info, "", ""));
sb.AppendLine(" System.Threading.ThreadState = " + t.ThreadState.ToString());
sb.AppendLine(" Call Stack:");
sb.Append(ListCallStack(t));
buffer.AppendLine(sb.ToString());
WriteBufferToFile();
}
/// <summary>
/// Log a summary of the state of all threads.
/// </summary>
/// <param name="info">Informational string to be pre-pended to output.</param>
public static void LogThreadStates(string info) {
if (!enabled) return;
StringBuilder sb = new StringBuilder();
sb.AppendLine(GetReport(info, "", ""));
foreach (ProcessThread pt in Process.GetCurrentProcess().Threads) {
sb.AppendLine(GetThreadState(pt));
}
buffer.AppendLine(sb.ToString());
WriteBufferToFile();
}
#endregion
#region Private methods
private static string GetReport(string source,
string what,
string extra) {
return GetReport(Thread.CurrentThread, source, what, extra);
}
private static string GetReport(Thread t,
string source,
string what,
string extra) {
string s = DateTime.Now.ToString("s") + " \t" +
t.Name + "(" + t.ManagedThreadId.ToString() + ")\t" +
source + "\t" + what + "\t" + extra;
return s;
}
private static string ListCallStack(Thread t) {
try {
StackTrace callStack = new StackTrace(t, true);
StringBuilder sb = new StringBuilder();
string s = "";
bool lastLineWasDots = false;
bool haveLoggedLocalFunc = false;
foreach (StackFrame frame in callStack.GetFrames()) {
s = GetMethodNameToLog(frame, ref haveLoggedLocalFunc);
if (string.IsNullOrEmpty(s)) {
//Ignore
} else if (s == "...") {
if (!lastLineWasDots)
sb.AppendLine(" ...");
lastLineWasDots = true;
} else {
sb.AppendLine(" " + s);
lastLineWasDots = false;
}
}
return sb.ToString();
} catch (Exception ex) {
return " No call stack available. " + ex.Message;
}
}
private static string GetMethodNameToLog(StackFrame f,
ref bool stripSysMethods) {
MethodBase method = f.GetMethod();
//Don't log stuff that's in this assembly.
if (method.DeclaringType.Assembly == Assembly.GetExecutingAssembly())
return "";
//Stuff in system assemblies we summarise after any local method.
if (IsSystemMethod(method)) {
if (stripSysMethods)
return "...";
} else {
stripSysMethods = true;
}
return GetMethodDescription(method) + GetShortFileName(f) + GetLineNumber(f);
}
private static bool IsSystemMethod(MethodBase mb) {
return mb.DeclaringType.FullName.StartsWith("System", StringComparison.CurrentCultureIgnoreCase);
}
private static string GetMethodDescription(MethodBase method) {
StringBuilder sb = new StringBuilder();
sb.Append(method.DeclaringType.FullName + "." + method.Name + "(");
ParameterInfo[] pInfos = method.GetParameters();
if (pInfos.Length > 0) {
foreach (ParameterInfo pi in pInfos) {
sb.Append(pi.ParameterType.Name + ", ");
}
sb.Remove(sb.Length - 2, 2);
}
sb.Append(")");
return sb.ToString();
}
private static string GetShortFileName(StackFrame f) {
string file = f.GetFileName();
if (string.IsNullOrEmpty(file))
return string.Empty;
string here = Path.GetFileName(Path.GetDirectoryName(file));
return " in '" + Path.Combine(here, Path.GetFileName(file)) + "'";
}
private static string GetLineNumber(StackFrame f) {
int i = f.GetFileLineNumber();
if (i == 0)
return string.Empty;
return " at line " + i.ToString();
}
private static void WriteBufferToFile() {
try {
using (StreamWriter w = File.AppendText(fileName)) {
w.Write(buffer.ToString());
buffer.Length = 0;
w.Flush();
w.Close();
}
} catch {
//Do nothing.
}
}
private static string GetThreadState(ProcessThread pt) {
if (pt == null)
return "";
StringBuilder sb = new StringBuilder();
sb.AppendLine(" Thread ID='" + pt.Id + "' Start=" + pt.StartTime.ToString("s"));
sb.Append(" System.Diagnostics.ThreadState = " + pt.ThreadState.ToString());
if (pt.ThreadState == System.Diagnostics.ThreadState.Wait)
sb.Append(" (ThreadWaitReason=" + pt.WaitReason.ToString() + ")");
return sb.ToString();
}
#endregion
}
}