3

按照这个问题的思路,我想创建一个 HttpModule 来为我们记录一些自定义的请求和响应。使用该问题的最流行答案中的代码,我已经启动并运行了一个 HttpModule,它确实有效:

class PortalTrafficModule : IHttpModule
{
    public void Dispose()
    {
        // Do Nothing
    }

    public void Init(HttpApplication context)
    {
        context.BeginRequest += new EventHandler(context_BeginRequest);
        context.EndRequest += new EventHandler(context_EndRequest);
    }

    private void context_BeginRequest(object sender, EventArgs e)
    {
        HttpContext context = ((HttpApplication)sender).Context;

        // Create and attach the OutputFilterStream to the Response and store it for later retrieval.
        OutputFilterStream filter = new OutputFilterStream(context.Response.Filter);
        context.Response.Filter = filter;
        context.Items.Add("OutputFilter", filter);

        // TODO: If required the request headers and content could be recorded here
    }

    private void context_EndRequest(object sender, EventArgs e)
    {
        HttpContext context = ((HttpApplication)sender).Context;
        OutputFilterStream filter = context.Items["OutputFilter"] as OutputFilterStream;

        if (filter != null)
        {
            // TODO: Log here - for now just debug.
            Debug.WriteLine("{0},{1},{2}",
                context.Response.Status,
                context.Request.Path,
                filter.ReadStream().Length);
        }
    }
}

(注意,代码中引用的OutputFilterStream类在引用的问题中)。

但是,响应似乎缺少我在 Fiddler 中看到的一些 HTTP 标头(例如“日期”),更重要的是,当我打开压缩时,我正在记录的响应没有被压缩,而我在 Fiddler 中看到的是。

所以我的问题- 是否可以记录压缩内容,或者这是否发生在我的模块无法连接的后续步骤中?

作为记录,我也尝试过处理PreSendRequestContent事件并且响应仍然未压缩。

4

1 回答 1

0

嗨,虽然我不能直接回答你的问题,但我过去曾做过类似的事情,并且发现以下资源非常有帮助和启发。最后,通过在 web config 中配置 System.Diagnostics 节点并创建流量跟踪日志,我设法实现了原始肥皂头所需的内容。我了解您的需求可能比这更细化,但我相信此资源可能仍会有所帮助。

http://msdn.microsoft.com/en-us/library/ms731859

特别感兴趣的可能是上面的消息日志配置和查看消息日志链接。

于 2012-08-01T22:23:42.077 回答