我怎么能在我的ASP.NET MVC3应用程序日志都Request.InputStream和Response.OutputStream流量的具体行动? [英] How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions?

查看:804
本文介绍了我怎么能在我的ASP.NET MVC3应用程序日志都Request.InputStream和Response.OutputStream流量的具体行动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关特定的一组操作的,我需要登录进入的请求的InputStream以及即将离任的Response.OutputStream。

For a specific set of Actions I am required to log the incoming Requests InputStream as well as the outgoing Response.OutputStream.

我设想使用ActionFilterAttribute,这和覆盖OnActionExecuted和OnResultExecuted方法。

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

所以我开始这个想法......

So I'm starting 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'll just hook this up with the Enterprise Library logging since I'm already using it for Error logging.


  1. 我是访问输入和输出流在适当的时候?

  2. 我可以使用企业库来轻松登陆流?

  3. 有一个完全不同的,更好的解决我的问题?

谢谢!

推荐答案

为了捕捉你可以使用的响应滤波器

In order to capture the response you could use a response filter:

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

,那么你可以有一个自定义操作属性:

then you could have a custom action attribute:

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

这篇关于我怎么能在我的ASP.NET MVC3应用程序日志都Request.InputStream和Response.OutputStream流量的具体行动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆