我正在研究IIS中的堆栈跟踪,所以我创建了一个WebApi项目,其端点会引发异常。
如果我抛出一个普通的异常:
throw new Exception("demo bug - Exception");
我得到一个完整的堆栈跟踪,如下所示:
{
"Message": "An error has occurred.",
"ExceptionMessage": "demo bug - Exception",
"ExceptionType": "System.Exception",
"StackTrace": " at DemoBug.Controllers.DefaultController.FooBar() in C:\\Workspace\\DemoBug\\DemoBug\\Controllers\\DefaultController.cs:line 24\r\n at DemoBug.Controllers.DefaultController.Bar() in C:\\Workspace\\DemoBug\\DemoBug\\Controllers\\DefaultController.cs:line 19\r\n at DemoBug.Controllers.DefaultController.Foo() in C:\\Workspace\\DemoBug\\DemoBug\\Controllers\\DefaultController.cs:line 14\r\n at DemoBug.Controllers.DefaultController.Get() in C:\\Workspace\\DemoBug\\DemoBug\\Controllers\\DefaultController.cs:line 30\r\n at lambda_method(Closure , Object , Object[] )\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\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.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"
}
但是如果我抛出一个HttpResponseException:
throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "demo bug - HttpResponseException"));
然后我只得到Message
部分:
{
"Message": "demo bug - HttpResponseException"
}
如何使用HttpResponseException包含堆栈跟踪?