我正在我的项目中编写一个异常测试过滤器,我想在其中记录请求和响应对象。这是我的代码:
public override void OnException(HttpActionExecutedContext actionExecutedContext)
{
//Add custom log
LogEventInfo eventInfo = new LogEventInfo();
eventInfo.TimeStamp = DateTime.Now;
eventInfo.Level = LogLevel.Error;
eventInfo.Exception = actionExecutedContext.Exception;
eventInfo.Properties["STATUS"] = actionExecutedContext.Response != null ? actionExecutedContext.Response.StatusCode : System.Net.HttpStatusCode.InternalServerError;
try
{
eventInfo.Properties["PAYLOAD"] = "";
if (actionExecutedContext.Response != null && actionExecutedContext.Response.StatusCode != System.Net.HttpStatusCode.OK)
{
eventInfo.Properties["PAYLOAD"] += JsonConvert.SerializeObject(actionExecutedContext.Request, Formatting.None,
new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
eventInfo.Properties["PAYLOAD"] += JsonConvert.SerializeObject(actionExecutedContext.Response, Formatting.None,
new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
}
}
catch (Exception ex)
{
eventInfo.Properties["PAYLOAD"] = "Logging Exception: " + ex.Message;
}
_logger.Log(eventInfo);
}
在执行上述代码时,它始终会引发异常从“ Microsoft.Owin.Host.SystemWeb.CallStreams.InputStream”上的“ ReadTimeout”获取值时出错。。谁能建议这个问题的任何解决方案?如何序列化 HttpActionExecutedContext 的 Request 和 Response 对象?