Reputation: 256
Back when I used synchronous methods in code using ASP.NET MVC 3, if an exception was thrown, it wasn't hard to figure out where it came from in my exception filter. Just looking at the stack trace was enough.
When an async
method in ASP.NET Web API throws an exception, however, the exception details are less useful, since the stack trace doesn't show where the exception was thrown from:
System.ArgumentException: title must not be empty.
Parameter name: title
at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__1.MoveNext()
--- 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.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()
--- 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.Web.Http.ApiController.<InvokeActionWithExceptionFilters>d__1.MoveNext()
Currently, I have to look at all the exceptions with this message and guess which one was thrown based on the request URI and what's going on behind the scenes, or try and reproduce the bug locally with a debugger attached.
MSDN has an article detailing a couple strategies for getting a casualty chain: http://msdn.microsoft.com/en-us/magazine/jj891052.aspx3. From my understanding, I have two choices:
await
callIs there a better way to log more helpful details about what threw this exception? Having the casualty chain would be nice, but just getting the exception source would be fine (what type and method this was thrown in).
Upvotes: 7
Views: 2087
Reputation: 3431
Compile against .NET 4.5.1 rather than 4.5 and you will generally get better exception traces with async.
Upvotes: 2
Reputation: 99909
If you deploy the PDB for your assembly to the same folder where the DLL is deployed, the exception stack trace should include file and line number information. In particular, the MoveNext()
calls will be located within your async
methods.
If optimizations are enabled for your assembly (and they likely are for Release builds), the line numbers may be occasionally inaccurate, so keep that in mind if you still can't figure out the problem based on the line number it tells you.
Upvotes: 0
Reputation: 456717
Both of your options cause run-time overhead.
A third option (which also causes run-time overhead) is to use my AsyncDiagnostics library. I'm partial to that one (naturally), but every option introduces run-time overhead.
Upvotes: 3