在 ASP.NET 和 IIS7 中记录原始和压缩的 HTTP 响应



按照这个问题的思路,我想创建一个 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事件,响应仍未压缩。

嗨,

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

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

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

最新更新