本文介绍了IExceptionLogger是否废弃了对Web API 2中ExceptionFilterAttribute的需求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在关于实现 IExceptionLogger 的官方文档()是否有任何理由注册全局 ExceptionFilterAttribute 如果您为注册服务IExceptionLogger

Following the discussion from the official documentation on implementing an IExceptionLogger (http://www.asp.net/web-api/overview/testing-and-debugging/web-api-global-error-handling) which links to the (now dated?) article on implementing an ExceptionFilterAttribute (http://www.asp.net/web-api/overview/testing-and-debugging/exception-handling), is there any reason to register a global ExceptionFilterAttribute if you register a service for IExceptionLogger?

我在调试控制器动作中生成的异常时,两个实现都处理了异常。所以 IExceptionLogger 因为文章引用的所有原因都是优越的。我们应该考虑 ExceptionFilterAttribute 已弃用?如果不是,为什么不呢?

I did and when debugging an exception generated in a controller action, both implementations handled the exception. So IExceptionLogger is superior for all the reasons cited in the article. Should we consider ExceptionFilterAttribute deprecated? If not, why not?

推荐答案

重新审视此主题,我终于有了更好的了解 IExceptionLogger ExceptionFilterAttribute IExceptionHandler

Revisiting this topic, I finally have a better understanding of the relationship between IExceptionLogger, ExceptionFilterAttribute, and IExceptionHandler.

来自不同的文档来源从问题:

和问题的实际答案:

说明各种类/接口(从 ExceptionFilterResult 反编译)之间的关系的代码块:

And the relevant block of code that illustrates the relationship between the various classes/interfaces (decompiled from ExceptionFilterResult):

public async Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
  ExceptionDispatchInfo exceptionInfo;
  try
  {
    return await this._innerResult.ExecuteAsync(cancellationToken);
  }
  catch (Exception ex)
  {
    exceptionInfo = ExceptionDispatchInfo.Capture(ex);
  }
  Exception exception = exceptionInfo.SourceException;
  bool isCancellationException = exception is OperationCanceledException;
  ExceptionContext exceptionContext = new ExceptionContext(exception, ExceptionCatchBlocks.IExceptionFilter, this._context);
  if (!isCancellationException)
    await this._exceptionLogger.LogAsync(exceptionContext, cancellationToken);
  HttpActionExecutedContext executedContext = new HttpActionExecutedContext(this._context, exception);
  for (int i = this._filters.Length - 1; i >= 0; --i)
  {
    IExceptionFilter exceptionFilter = this._filters[i];
    await exceptionFilter.ExecuteExceptionFilterAsync(executedContext, cancellationToken);
  }
  if (executedContext.Response == null && !isCancellationException)
    executedContext.Response = await this._exceptionHandler.HandleAsync(exceptionContext, cancellationToken);
  if (executedContext.Response != null)
    return executedContext.Response;
  if (exception == executedContext.Exception)
    exceptionInfo.Throw();
  throw executedContext.Exception;
}

这篇关于IExceptionLogger是否废弃了对Web API 2中ExceptionFilterAttribute的需求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 05:23