如何在异常处理程序中获取请求JSON消息 - webapi? [英] How to get request JSON message with in the exception handler - webapi?
本文介绍了如何在异常处理程序中获取请求JSON消息 - webapi?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了webApi全局异常处理程序,我必须在prod环境中记录请求参数/ JSON以验证请求是否发生异常,请告诉我在<$ p $中记录请求消息的方法p> ExceptionHandler
。
WebApiConfig
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Services.Replace(typeof(IExceptionHandler),
new GlobalExceptionHandler());
}
}
GlobalExceptionHandler
< br $>
public class GlobalExceptionHandler:ExceptionHandler
{
public override void Handle(ExceptionHandlerContext context)
{
Logger.log(Exception:\t+ context.Exception.Message)
Logger.log(Request JSON:\t+ Josn.Serializer(context.Request.Content));
}
}
我的尝试:
i试图从
获取JSON ExceptionHandlerContext
解决方案
字符串数据;
使用(var stream = ExceptionHandlerContext.Request.content.ReadAsStreamAsync()。result)
{
if(stream.CanSeek)< br $>
{
stream.Position = 0;
}
data = ExceptionHandlerContext.Request.content.ReadAsStreamAsync() 。
}
JsonConvert.DeserializObject< Dictionary< string,string>>(数据)
使用NewtonSoft.Json
I created webApi global exception handler, i have to log the request parameters/JSON in the prod environment to verify the request if any exception happened, Please let me know the way to log the request message with in the
ExceptionHandler
.
WebApiConfig
public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.Services.Replace(typeof (IExceptionHandler), new GlobalExceptionHandler()); } }
GlobalExceptionHandler
public class GlobalExceptionHandler : ExceptionHandler { public override void Handle(ExceptionHandlerContext context) { Logger.log("Exception : \t" + context.Exception.Message) Logger.log("Request JSON : \t" + Josn.Serializer(context.Request.Content)); } }
What I have tried:
i tried to get the JSON from
ExceptionHandlerContext
解决方案
String data;
Using (var stream = ExceptionHandlerContext.Request.content.ReadAsStreamAsync().result )
{
if(stream.CanSeek)
{
stream.Position = 0;
}
data = ExceptionHandlerContext.Request.content.ReadAsStreamAsync().result
}
JsonConvert.DeserializObject<Dictionary<string,string>>(data)
using NewtonSoft.Json
这篇关于如何在异常处理程序中获取请求JSON消息 - webapi?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文