ASP.NET Web API全局异常处理 - 堆栈跟踪可读性

时间:2018-04-09 04:50:34

标签: c# asp.net asp.net-web-api exception-handling

我正在开发一个ASP.Net Web API项目(.Net Framework 4.7.1)

我正在尝试使用ExceptionLogger来全局处理异常并记录它们

我的实现如下:

public class MyExceptionLogger : ExceptionLogger
{
    public override void Log(ExceptionLoggerContext context)
    {
        Console.Error.WriteLine(context.Exception);
    }
}

我将它订阅到WebApiConfig.cs中,如下所示:

public static void Register(HttpConfiguration config)
{
    // Configuration et services API Web
    config.Services.Add(typeof(IExceptionLogger), new MyExceptionLogger());

    [...]
}

它很好地捕获了来自控制器的未捕获异常,但堆栈跟踪似乎过于冗长:

   at WebApplication.Controllers.ValuesController.Get() in c:\users\ts-sylvain.pajaud\source\repos\WebApplication\WebApplication\Controllers\ValuesController.cs:line 15
   at lambda_method(Closure , Object , Object[] )
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
--- 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.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.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.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()

来自“ ---从先前位置抛出异常的堆栈跟踪结束--- ”的部分使堆栈跟踪变得沉重且不易读取 它似乎与某些异步代码有关,但我的控制器没有任何异步代码:

public class ValuesController : ApiController
{
    // GET api/values
    public IEnumerable<string> Get()
    {
        throw new Exception("Test");
        return new string[] { "value1", "value2" };
    }
}

有没有办法让“更易读”的堆栈跟踪?

我检查了这个帖子但是没有一个解释的解决方案适用于我的项目: ExceptionFilter stack trace in synchronous action scenario

2 个答案:

答案 0 :(得分:0)

每个Exception可能都有InnerException,因此您还需要检查InnerException

while(exception != null)
{
   Console.Error.WriteLine(exception);
   if(exception.InnerException != null)
      exception = exception.InnerException;
}

修改1:

Ben Adams最近开发了一个可以使异常更具可读性的软件包: enter image description here https://github.com/benaadams/Ben.Demystifier

答案 1 :(得分:0)

您可以使用System.Diagnostic中的StackTrace类,然后您可以将异常写入其他位置。此类可帮助您使用fileNames和行号

读取stackTrace

https://msdn.microsoft.com/en-us/library/system.diagnostics.stacktrace(v=vs.110).aspx