0

I am writing one Exception test filter in my project where I want to log the Request and Response object. Here is my code:

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);
}

While executing the above code it always throws exception Error getting value from 'ReadTimeout' on 'Microsoft.Owin.Host.SystemWeb.CallStreams.InputStream'. Can anyone suggest any solution for this problem? How can I serialize the Request and Response object of HttpActionExecutedContext?

smn.tino
  • 2,272
  • 4
  • 32
  • 41
Amrendra
  • 511
  • 8
  • 18

0 Answers0