如何 return ExceptionLogger 中的自定义响应

How to return a custom response inside the ExceptionLogger

Log 方法内部没有 context.Response = MyResponse;

我怎样才能 return 像这样的自定义回复:

当调用我的 Log 方法时,我想将 id 传递给当前请求。问题是我如何获取请求的内容实例,并向其传递 id?

这就是导致异常的操作:

   public async Task<IHttpActionResult> GetConfiguredProducts(int number)
    {
        var products = await service.Get(number);
        return Ok(products);
    }


public class GlobalExceptionLogger : ExceptionLogger
    {
        private static int id;

        public override void Log(ExceptionLoggerContext context)
        {
            Interlocked.Increment(ref id);
            loggingService.ErrorException(string.Format("id: {0}", id), context.Exception);

        }
    }

如果您使用的是基于 System.Web 的堆栈(如果您使用的是 IIS 并且 ASP.NET 版本 < 5 可能就是这种情况),您可以获得对请求和响应的引用在任何地方使用

HttpContext.Current 

对象。

ExceptionLoggerContext 公开了 ExceptionContext,它公开了 Request 和 RequestContext。够了吗?