从全局错误处理程序检索Request.Content的正确方法

时间:2015-01-05 14:45:26

标签: error-handling asp.net-web-api2

我已经注册了一个全局异常处理程序,它会触发并包含我需要的所有信息,但Request.Content除外,它总是为空的......我需要在调试时传入的值。 ..

Public class MyExceptionLogger : ExceptionLogger
    {
        public override void Log(ExceptionLoggerContext context)
        {

            try
            {
                 ... other code

            var methodName = context.Request.Method.ToString();
            var errorUri = context.Request.RequestUri.ToString();
            var errorMessage = context.Exception.Message;
            var errorStackTrace = context.Exception.StackTrace.ToString();

            var payload = context.Request.Content.ReadAsStringAsync().Result;

            .....  other code  

            }

       }
   }

从全局错误处理程序检索Request.Content的正确方法是什么?在上面的代码中,Content属性已经被模型绑定器读取,因此总是为空。

如何从异常中始终获取已发布的正文?

我应该在自定义MessageHandler中检索并保存已发布的正文吗?

由于 格雷格

1 个答案:

答案 0 :(得分:1)

尝试在自定义消息处理程序中读取request.Content的缓冲区..如果我用这段代码读取它:

var payload = context.Request.Content.ReadAsStringAsync().Result;

并且没有使用它...当模型绑定器读取缓冲区时缓冲区将不会被清空,因为它现在总是在我的异常记录器中...我不知道这是设计还是什么但它很恼火!