title: Stack traces with async/await
body: <p>It's clear why stack traces are affected with Microsoft's new programming paradigm. We now have a semantic stack and a couple of physical ones (my choice of words).</p>
  
  <p>What I get to see is an exception's <code>StackTrace</code> property (and in the debugger) is the physical ones, concatenated:</p>
  
  <pre><code>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(() =&gt; Thread.Sleep(1000));
      await Bar();
      await Task.Factory.StartNew(() =&gt; Thread.Sleep(1000));
  }
  private async Task Bar()
  {
      await Task.Factory.StartNew(() =&gt; Thread.Sleep(1000));
      throw new Exception();
      await Task.Factory.StartNew(() =&gt; Thread.Sleep(1000));
  }
  </code></pre>
  
  <p>This gives:</p>
  
  <pre><code>StackTrace of last exception:    at NLogAsyncExceptionTestCase.Program.&lt;Bar&gt;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.&lt;Foo&gt;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.&lt;CheckFooAndBar&gt;d__0.MoveNext() in c:\Users\Jens\Documents\Visual Studio 2012\Projects\NLogAsyncExceptionTestCase\NLogAsyncExceptionTestCase.Console\Program.cs:line 30 
  </code></pre>
  
  <p>My question is: Is there a (convenient, standard) way to convert this to a proper backtrace in the semantic sense, such as:</p>
  
  <pre><code>CheckFooAndBar
  Foo
  Bar
  </code></pre>
  
  <p>Of course there could be a mixture of awaits and inline path fragments in the stack.</p>
  
  <p>I tried looking at the stack as it is with .NET 4.5 and SL5 with the async targetting pack, but not yet with WinRT. The output is from .NET 4.5.</p>
  
  <p>In SL5, which is what I mainly do, the situation is more problematic: You don't get line numbers in stack traces in Silverlight (even with elevated privileges), which makes the need for context more important.</p>