很清楚为什么堆栈跟踪会受到微软新的编程范式的影响。我们现在有一个语义堆栈和几个物理堆栈(我选择的单词)。
我看到的是异常的StackTrace
属性(在调试器中)是物理属性,串联:
private async Task CheckFooAndBar()
{
var log = LogManager.GetLogger("Test");
log.Info("CheckFooAndBar");
try
{
await Foo();
}
catch (Exception ex)
{
log.Info("StackTrace of last exception: " + ex.StackTrace);
}
Console.ReadKey();
}
private async Task Foo()
{
await Task.Factory.StartNew(() => Thread.Sleep(1000));
await Bar();
await Task.Factory.StartNew(() => Thread.Sleep(1000));
}
private async Task Bar()
{
await Task.Factory.StartNew(() => Thread.Sleep(1000));
throw new Exception();
await Task.Factory.StartNew(() => Thread.Sleep(1000));
}
这给出了:
StackTrace of last exception: at NLogAsyncExceptionTestCase.Program.<Bar>d__d.MoveNext() in c:\Users\Jens\Documents\Visual Studio 2012\Projects\NLogAsyncExceptionTestCase\NLogAsyncExceptionTestCase.Console\Program.cs:line 53
--- 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 NLogAsyncExceptionTestCase.Program.<Foo>d__8.MoveNext() in c:\Users\Jens\Documents\Visual Studio 2012\Projects\NLogAsyncExceptionTestCase\NLogAsyncExceptionTestCase.Console\Program.cs:line 44
--- 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 NLogAsyncExceptionTestCase.Program.<CheckFooAndBar>d__0.MoveNext() in c:\Users\Jens\Documents\Visual Studio 2012\Projects\NLogAsyncExceptionTestCase\NLogAsyncExceptionTestCase.Console\Program.cs:line 30
我的问题是:是否有一种(方便的、标准的)方法可以将其转换为语义上的适当回溯,例如:
CheckFooAndBar
Foo
Bar
当然,堆栈中可能混合有等待和内联路径片段。
我尝试使用带有异步目标包的 .NET 4.5 和 SL5 查看堆栈,但尚未使用 WinRT。输出来自 .NET 4.5。
在我主要做的 SL5 中,情况更成问题:您不会在 Silverlight 中的堆栈跟踪中获得行号(即使具有提升的权限),这使得对上下文的需求变得更加重要。