How can I register Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 application for specific actions?

For a specific set of actions, I need to register incoming InputStream requests, as well as outgoing Response.OutputStream.

I suggest using ActionFilterAttribute for this and overriding the OnActionExecuted and OnResultExecuted methods.

So, I start with this idea ...

public class ActionLoggerAttribute : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        base.OnActionExecuted(filterContext);

        HttpRequestBase request = filterContext.HttpContext.Request;
        // TODO: Log the Request.InputStream
    }

    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        base.OnResultExecuted(filterContext);

        HttpResponseBase response = filterContext.HttpContext.Response;
        // TODO: Log the Response.OutputStream
    }
}

Ideally, I just connect this to the Enterprise Library journal, as I already use it to log errors.

  • Am I accessing input and output streams at the appropriate time?
  • Can I use the corporate library to easily record streams?
  • Is there a completely different and better solution to my problem?

Thank!

0
1

, :

public class CaptureResponse : MemoryStream
{
    private readonly Stream _stream;
    public CaptureResponse(Stream stream)
    {
        _stream = stream;
    }

    public override void Write(byte[] buffer, int offset, int count)
    {
        // TODO: Log the response buffer here 
        // (note that it could be a chunk)

        _stream.Write(buffer, offset, count);
    }
}

:

public class ActionLoggerAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var request = filterContext.HttpContext.Request;
        var response = filterContext.HttpContext.Response;
        response.Filter = new CaptureResponse(response.Filter);

        // TODO: Log the Request.InputStream

        base.OnActionExecuting(filterContext);
    }
}
+1

All Articles