正确的堆栈跟踪.. Net Web Api异步/任务调用

本文关键字:异步 Api 任务 调用 Web Net 堆栈 跟踪 | 更新日期: 2023-09-27 18:07:04

当我使用Web Api调用客户端时发生错误。问题是我得到的堆栈跟踪有点不可读,没有行号,所以我不确定在哪里看。这是作为Json响应返回的。

 "message": "An error has occurred.",
"exceptionMessage": "Object reference not set to an instance of an object.",
"exceptionType": "System.NullReferenceException",
"stackTrace": "   at WakeSocial.Gateway.WebApi.Host.Controllers.NotificationsController.<Response>d__7.MoveNext()'r'n--- End of stack trace from previous location where exception was thrown ---'r'n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)'r'n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)'r'n   at System.Threading.Tasks.TaskHelpersExtensions.<CastToObject>d__3`1.MoveNext()'r'n--- End of stack trace from previous location where exception was thrown ---'r'n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)'r'n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)'r'n   at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()'r'n--- End of stack trace from previous location where exception was thrown ---'r'n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)'r'n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)'r'n   at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()'r'n--- End of stack trace from previous location where exception was thrown ---'r'n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)'r'n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)'r'n   at System.Web.Http.Filters.AuthorizationFilterAttribute.<ExecuteAuthorizationFilterAsyncCore>d__2.MoveNext()'r'n--- End of stack trace from previous location where exception was thrown ---'r'n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)'r'n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)'r'n   at System.Web.Http.Controllers.AuthenticationFilterResult.<ExecuteAsync>d__0.MoveNext()'r'n--- End of stack trace from previous location where exception was thrown ---'r'n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)'r'n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)'r'n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"
}

是否有一种方法可以配置web api返回错误发生的行号,以便我可以更适当地调试问题?

这里讨论了这个问题:使用async/await进行堆栈跟踪

我目前使用的是。net 4.5,但错误仍然以这种方式输出。

正确的堆栈跟踪.. Net Web Api异步/任务调用

我使用一个扩展,存储堆栈跟踪信息异常,例如,原始代码:

try{    
   await NestedAsyncOperation();
}catch(NullReferenceException e){
  Debug.WriteLine(e)
}

带有可读堆栈跟踪:

try{    
   await NestedAsyncOperation().Trace();
}catch(Exception e){
   e.Catch((NullReferenceException ex) => {
     Debug.WriteLine(e);
   }).RethrowUnhandled();
}

和输出将包含带有行号的堆栈跟踪。查看更多https://github.com/ilio/AsyncStackTrace/blob/master/UnitTests/AsyncStackTraceExtensionUnitTest.cs