10

由于 .NET 4.5 之前的运行时(包括 SL/WP)不支持异步,它们生成的堆栈跟踪显示编译器生成的类/方法名称(如d_15)。

有谁知道给定运行时堆栈跟踪、程序集和 pdb生成更好堆栈跟踪的实用程序?

需要明确的是:我不是在寻找完整的异步堆栈,只是更好地了解实际引发异常的方法

上面的说法似乎不够清楚,所以这里举个例子:

public async void Foo()
{
    await Bar();
}

public async Task Bar()
{
    async SomethingToMakeThisMethodAsync();

    throw new Exception()
}

当抛出异常时Bar,堆栈跟踪将只包含生成的方法名称 ( d_15())。我不在乎那个叫 Bar 的 Foo。我只想知道 Bar 是引发异常的方法

4

2 回答 2

4

Andrew Stasyuk 在 MSDN 杂志http://msdn.microsoft.com/en-us/magazine/jj891052.aspx中有一篇很棒的文章,其中详细介绍了异步因果链作为一种帮助调试的方法,因为堆栈跟踪不连贯且令人困惑。

于 2013-05-05T23:47:03.580 回答
1

Seems like you wasted your rep on bounty since this question was asked few times before, for example, in:

which, as I can recall, had few bounties but had not gotten much further than ref to:

Or in this SO question:

with reference in answer to:

Another similar SO question:

I found the collection of references on Parallel Debugging (Parallel Stacks, Parallel Tasks) in Visual Studio 2010 by Daniel Moth to be very helpful

Update:

Having run your code

using System;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
        Foo();
    }
    public static async void Foo()
    {
      await Bar();
    }

    public static async Task Bar()
    {
      try
      {
        //async SomethingToMakeThisMethodAsync();
        //to make this async
        await TaskEx.Delay(2000);//await Task.Delay(2000);//in .NET 4.5
        throw new Exception();
      }
      catch (Exception)
      {
        throw new Exception("This is Excptn in Bar() method");
      }
    }
  }
}

in VS2010+Async CTP debug mode (F5), I clearly see my identifying exception message "This is Excptn in Bar() method":

enter image description here

enter image description here

Anyway, it is identified as ConsoleApplication1.Program.<Bar> in stack trace (Locals window) even without any additional marking (catching-rethrowing exception with custom message), i.e. having substituted in above code the Bar() method by:

public static async Task Bar()
{
    //async SomethingToMakeThisMethodAsync();
    //to make this async
     await TaskEx.Delay(2000);//await Task.Delay(2000);//in .NET 4.5
     throw new Exception();
}

I see in stack trace that exception was thrown in ConsoleApplication1.Program.<Bar>:

+ $exception    
{System.Exception: Exception of type 'System.Exception' was thrown.

Server stack trace: 
   at ConsoleApplication1.Program.<Bar>d__3.MoveNext() in R:\###Debugging\#seUnmangling (pre .NET 4.5) asyncawait stack traces\AsyncConsole_Sln\1Cons_Prj\Program.cs:line 29

Exception rethrown at [0]: 
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at ConsoleApplication1.Program.<Foo>d__0.MoveNext() in R:\###Debugging\#seUnmangling (pre .NET 4.5) asyncawait stack traces\AsyncConsole_Sln\1Cons_Prj\Program.cs:line 19

Exception rethrown at [1]: 
   at System.Runtime.CompilerServices.AsyncVoidMethodBuilder.<SetException>b__1(Object state)
   at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()}   System.Exception

enter image description here

于 2013-05-06T10:57:55.727 回答