Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
214 lines (201 loc) · 9.93 KB
/
Copy pathProgram.cs
File metadata and controls
214 lines (201 loc) · 9.93 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
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using AsyncStackTrace;
namespace Windows45
{
class Program
{
static void Main()
{
do
{
switch (DisplayMenu())
{
case 1:
ThrowStandardException().Wait();
break;
case 2:
ThrowAsyncStackTraceAsyncOnly().Wait();
break;
case 3:
ThrowAsyncStackTraceFull().Wait();
break;
case 4:
ThrowAsyncStackTraceOut().Wait();
break;
default:
Console.WriteLine("Inalid input. 1-5 expected.");
break;
case 5:
return;
}
} while (true);
}
private static async Task ThrowStandardException()
{
try
{
await Task.Delay(0);
var nestedTask = Task.Factory.StartNew(() =>
{
// create nested task throws exception
throw new ArgumentException("Example exception");
});
await nestedTask;
}
catch (ArgumentException ee)
{
Debug.WriteLine("Standard exception:\r\n" + ee);
Console.WriteLine("Standard exception:\r\n" + ee);
/**
output:
Standard exception:
System.ArgumentException: Example exception
at Windows45.Program.<ThrowStandardException>b__0() in d:\Documents\Visual Studio 2013\Projects\AsyncStackTrace\Examples\Windows\Windows45\Program.cs:line 47
at System.Threading.Tasks.Task.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Windows45.Program.<ThrowStandardException>d__2.MoveNext() in d:\Documents\Visual Studio 2013\Projects\AsyncStackTrace\Examples\Windows\Windows45\Program.cs:line 49
*/
}
}
private static async Task ThrowAsyncStackTrace()
{
await Task.Delay(0).Trace(); // do nothing, just for example, you must add .Trace() after every awaited task.
var nestedTask = Task.Factory.StartNew(() =>
{
// create nested task throws exception
throw new ArgumentException("Example exception").Trace(); // Must trace exceptions too
});
await nestedTask.Trace();
}
private static async Task ThrowAsyncStackTraceAsyncOnly()
{
try
{
await ThrowAsyncStackTrace();
}
catch (Exception ee)
{
ee.Catch(delegate(ArgumentException exception)
{
Debug.WriteLine("AsyncStackTrace exception:\r\n" + exception.GetAsyncTrace());
Console.WriteLine("AsyncStackTrace exception:\r\n" + exception.GetAsyncTrace());
})
.RethrowUnhandled();
/*
output:
AsyncStackTrace exception:
ArgumentException: Example exception
at ThrowAsyncStackTrace in d:\Documents\Visual Studio 2013\Projects\AsyncStackTrace\Examples\Windows\Windows45\Program.cs:line 68
at ThrowAsyncStackTrace in d:\Documents\Visual Studio 2013\Projects\AsyncStackTrace\Examples\Windows\Windows45\Program.cs:line 71
*/
}
}
private static async Task ThrowAsyncStackTraceFull()
{
try
{
await ThrowAsyncStackTrace();
}
catch (Exception ee)
{
// extract ArgumentException from AggregateException
ee.Catch(delegate(ArgumentException exception)
{
Debug.WriteLine("AsyncStackTrace exception:\r\n" + exception.GetFullTrace());
Console.WriteLine("AsyncStackTrace exception:\r\n" + exception.GetFullTrace());
/*
output:
AsyncStackTrace exception:
System.ArgumentException: Example exception
at Windows45.Program.<ThrowAsyncStackTrace>b__6() in d:\Documents\Visual Studio 2013\Projects\AsyncStackTrace\Examples\Windows\Windows45\Program.cs:line 68
at System.Threading.Tasks.Task.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at AsyncStackTrace.AsyncStackTraceExtension.<Trace>d__0.MoveNext() in d:\Documents\Visual Studio 2013\Projects\AsyncStackTrace\AsyncStackTrace\AsyncStackTraceExtension.cs:line 100
--- Async stack trace:
at ThrowAsyncStackTrace in d:\Documents\Visual Studio 2013\Projects\AsyncStackTrace\Examples\Windows\Windows45\Program.cs:line 68
at ThrowAsyncStackTrace in d:\Documents\Visual Studio 2013\Projects\AsyncStackTrace\Examples\Windows\Windows45\Program.cs:line 71
*/
})
.RethrowUnhandled();
}
}
private static async Task ThrowAsyncStackTraceOut()
{
try
{
await ThrowAsyncStackTrace();
}
catch (Exception ee)
{
// ee is AggregateException contains ArgumentException
Debug.WriteLine("AsyncStackTrace exception:\r\n" + ee);
Console.WriteLine("AsyncStackTrace exception:\r\n" + ee);
/*
output:
AsyncStackTrace exception:
AsyncStackTrace.AggregateExceptionEx: Example exception ---> System.ArgumentException: Example exception
at Windows45.Program.<ThrowAsyncStackTrace>b__6() in d:\Documents\Visual Studio 2013\Projects\AsyncStackTrace\Examples\Windows\Windows45\Program.cs:line 68
at System.Threading.Tasks.Task.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at AsyncStackTrace.AsyncStackTraceExtension.<Trace>d__0.MoveNext() in d:\Documents\Visual Studio 2013\Projects\AsyncStackTrace\AsyncStackTrace\AsyncStackTraceExtension.cs:line 100
--- End of inner exception stack trace ---
at AsyncStackTrace.AsyncStackTraceExtension.<Trace>d__0.MoveNext() in d:\Documents\Visual Studio 2013\Projects\AsyncStackTrace\AsyncStackTrace\AsyncStackTraceExtension.cs:line 104
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Windows45.Program.<ThrowAsyncStackTrace>d__8.MoveNext() in d:\Documents\Visual Studio 2013\Projects\AsyncStackTrace\Examples\Windows\Windows45\Program.cs:line 71
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Windows45.Program.<ThrowAsyncStackTraceOut>d__16.MoveNext() in d:\Documents\Visual Studio 2013\Projects\AsyncStackTrace\Examples\Windows\Windows45\Program.cs:line 121
---> (Inner Exception #0) System.ArgumentException: Example exception
at Windows45.Program.<ThrowAsyncStackTrace>b__6() in d:\Documents\Visual Studio 2013\Projects\AsyncStackTrace\Examples\Windows\Windows45\Program.cs:line 68
at System.Threading.Tasks.Task.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at AsyncStackTrace.AsyncStackTraceExtension.<Trace>d__0.MoveNext() in d:\Documents\Visual Studio 2013\Projects\AsyncStackTrace\AsyncStackTrace\AsyncStackTraceExtension.cs:line 100<---
----->
---> (Inner Exception #0) ArgumentException: Example exception
at ThrowAsyncStackTrace in d:\Documents\Visual Studio 2013\Projects\AsyncStackTrace\Examples\Windows\Windows45\Program.cs:line 68
at ThrowAsyncStackTrace in d:\Documents\Visual Studio 2013\Projects\AsyncStackTrace\Examples\Windows\Windows45\Program.cs:line 71<---
<-----
*/
}
}
static public int DisplayMenu()
{
Console.WriteLine("Print stack trace for async task:");
Console.WriteLine();
Console.WriteLine("1. Standard");
Console.WriteLine("2. Async trace only");
Console.WriteLine("3. Full stack trace (standard + async)");
Console.WriteLine("4. Stack for unhandled exception");
Console.WriteLine("5. Exit");
Console.WriteLine();
Console.WriteLine("Press 1-5");
var result = Console.ReadLine();
int value;
Int32.TryParse(result, out value);
return value;
}
}
}