Repository navigation
Expand file tree
/
Copy pathAsyncStackTraceExtension.cs
More file actions
316 lines (292 loc) · 10.6 KB
/
Copy pathAsyncStackTraceExtension.cs
File metadata and controls
316 lines (292 loc) · 10.6 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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Threading.Tasks;
#if SUPPORT_ASYNC_INFO
using Windows.Foundation;
#endif
namespace AsyncStackTrace
{
public static class AsyncStackTraceExtension
{
#if SUPPORT_ASYNC_INFO
public static async Task Trace(
this IAsyncAction asyncOperation,
[CallerMemberName] string callerMemberName = null,
[CallerFilePath] string callerFilePath = null,
[CallerLineNumber] int callerLineNumber = 0)
{
try
{
await asyncOperation;
}
catch (Exception e)
{
throw AddAsyncStackTrace(e, callerMemberName, callerFilePath, callerLineNumber);
}
}
public static async Task<TResult> Trace<TResult, TProgress>(
this IAsyncOperationWithProgress<TResult, TProgress> asyncOperation,
[CallerMemberName] string callerMemberName = null,
[CallerFilePath] string callerFilePath = null,
[CallerLineNumber] int callerLineNumber = 0)
{
try
{
return await asyncOperation;
}
catch (Exception e)
{
throw AddAsyncStackTrace(e, callerMemberName, callerFilePath, callerLineNumber);
}
}
public static async Task Trace<TProgress>(
this IAsyncActionWithProgress<TProgress> asyncOperation,
[CallerMemberName] string callerMemberName = null,
[CallerFilePath] string callerFilePath = null,
[CallerLineNumber] int callerLineNumber = 0)
{
try
{
await asyncOperation;
}
catch (Exception e)
{
throw AddAsyncStackTrace(e, callerMemberName, callerFilePath, callerLineNumber);
}
}
public static async Task<T> Trace<T>(
this IAsyncOperation<T> asyncOperation,
[CallerMemberName] string callerMemberName = null,
[CallerFilePath] string callerFilePath = null,
[CallerLineNumber] int callerLineNumber = 0)
{
try
{
return await asyncOperation;
}
catch (Exception e)
{
throw AddAsyncStackTrace(e, callerMemberName, callerFilePath, callerLineNumber);
}
}
#endif
/// <summary>
/// Store stack information for untyped task
/// </summary>
/// <param name="task"></param>
/// <param name="callerMemberName"></param>
/// <param name="callerFilePath"></param>
/// <param name="callerLineNumber"></param>
/// <returns></returns>
public static async Task Trace(
this Task task,
[CallerMemberName] string callerMemberName = null,
[CallerFilePath] string callerFilePath = null,
[CallerLineNumber] int callerLineNumber = 0)
{
try
{
await task;
}
catch (Exception ex)
{
throw AddAsyncStackTrace(ex, callerMemberName, callerFilePath, callerLineNumber);
}
}
/// <summary>
/// Store stack trace for typed task
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="task"></param>
/// <param name="callerMemberName"></param>
/// <param name="callerFilePath"></param>
/// <param name="callerLineNumber"></param>
/// <returns></returns>
public static async Task<T> Trace<T>(
this Task<T> task,
[CallerMemberName] string callerMemberName = null,
[CallerFilePath] string callerFilePath = null,
[CallerLineNumber] int callerLineNumber = 0)
{
try
{
return await task;
}
catch (Exception ex)
{
throw AddAsyncStackTrace(ex, callerMemberName, callerFilePath, callerLineNumber);
}
}
/// <summary>
/// Store stack trace for exception
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="exception"></param>
/// <param name="callerMemberName"></param>
/// <param name="callerFilePath"></param>
/// <param name="callerLineNumber"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Exception Trace<T>(this T exception,
[CallerMemberName] string callerMemberName = null,
[CallerFilePath] string callerFilePath = null,
[CallerLineNumber] int callerLineNumber = 0) where T : Exception
{
StoreStackTrace(exception, callerMemberName, callerFilePath, callerLineNumber);
return exception;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Exception AddAsyncStackTrace(
Exception ex,
string callerMemberName,
string callerFilePath,
int callerLineNumber)
{
var exceptions = StoreStackTrace(ex, callerMemberName, callerFilePath, callerLineNumber);
var result = new AggregateExceptionEx(
ex.Message,
exceptions);
return result;
}
/// <summary>
/// Store stack trace information inside the exception (ex.Data["_AsyncStackTrace"])
/// </summary>
/// <param name="ex"></param>
/// <param name="callerMemberName"></param>
/// <param name="callerFilePath"></param>
/// <param name="callerLineNumber"></param>
/// <returns></returns>
private static IEnumerable<Exception> StoreStackTrace(Exception ex, string callerMemberName, string callerFilePath,
int callerLineNumber)
{
var aggregateException = ex as AggregateException;
IEnumerable<Exception> exceptions;
if (aggregateException != null)
{
var exception = aggregateException.Flatten();
exceptions = exception.InnerExceptions;
}
else
{
exceptions = new[] { ex };
}
foreach (var inner in exceptions)
{
var trace = inner.Data["_AsyncStackTrace"] as LinkedList<string> ?? new LinkedList<string>();
trace.AddLast(string.Format(" at {0} in {1}:line {2}", callerMemberName, callerFilePath, callerLineNumber));
inner.Data["_AsyncStackTrace"] = trace;
}
return exceptions;
}
/// <summary>
/// Catch only exceptions of type T
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="e"></param>
/// <param name="handler"></param>
/// <returns></returns>
public static Exception Catch<T>(this Exception e, Action<T> handler) where T : Exception
{
var aggregateException = e as AggregateException;
if (aggregateException != null)
{
var innerExceptions = aggregateException.InnerExceptions;
var unhandled = new List<Exception>(innerExceptions.Count);
foreach (var ex in innerExceptions)
{
var exception = ex as T;
if (exception == null)
{
unhandled.Add(ex);
continue;
}
handler(exception);
}
if (unhandled.Count > 0)
{
return new AggregateExceptionEx(e.Message, unhandled);
}
return null;
}
{
var exception = e as T;
if (exception == null)
{
return e;
}
handler(exception);
return null;
}
}
/// <summary>
/// Handle unhandled exceptions
/// </summary>
/// <param name="e"></param>
/// <param name="handler"></param>
public static void Unhandled(this Exception e, Action<Exception> handler)
{
if (e == null)
{
return;
}
handler(e);
}
/// <summary>
/// Rethrow unhandled exceptions
/// </summary>
/// <param name="e"></param>
public static void RethrowUnhandled(this Exception e)
{
if (e == null)
{
return;
}
ExceptionDispatchInfo.Capture(e).Throw();
}
/// <summary>
/// Only async stack trace
/// </summary>
/// <param name="ex"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string GetAsyncTrace(this Exception ex)
{
var exception = ex as AggregateException;
if (exception != null)
{
var innerExceptions = exception.InnerExceptions;
var builder = new StringBuilder();
for (int index = 0; index < innerExceptions.Count; index++)
{
var innerException = innerExceptions[index];
builder.Append("ERROR " + index + ": " + innerException.GetAsyncTrace());
}
return builder.ToString();
}
var trace = ex.Data["_AsyncStackTrace"] as LinkedList<string>;
if (trace == null)
{
return ex.ToString();
}
string prefix = ex.GetType().Name + ": " + ex.Message + "\r\n";
return prefix + string.Join("\r\n", trace);
}
/// <summary>
/// Appends async trace to end of original stack trace
/// </summary>
/// <param name="ex"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string GetFullTrace(this Exception ex)
{
var trace = ex.Data["_AsyncStackTrace"] as LinkedList<string>;
if (trace == null)
return string.Empty;
return string.Format("{0}\n--- Async stack trace:\n\t", ex)
+ string.Join("\n\t", trace);
}
}
}