gpt4 book ai didi

c# - 来自 Asp.Net Web Api 异步/任务调用的正确堆栈跟踪

转载 作者:太空狗 更新时间:2023-10-29 23:50:25 25 4
gpt4 key购买 nike

当我使用客户端调用 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 以返回发生错误的行号,以便我可以更适本地调试问题?

这里讨论了这个问题:Stack traces with async/await

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

最佳答案

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

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

关于c# - 来自 Asp.Net Web Api 异步/任务调用的正确堆栈跟踪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31897386/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com