Logging raw and compressed HTTP responses in ASP.NET & IIS7

烈酒焚心 提交于 2019-12-01 07:11:49

问题


Along the lines of this question I want to create a HttpModule that will do some custom logging of requests and responses for us. Using the code in the most popular answer for that question I've got a HttpModule up and running which does indeed work:

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

(note that the OutputFilterStream class referred to in the code is in the referenced question).

However, the responses seem to be missing some HTTP headers that I see in Fiddler (like "Date") and more importantly, when I turn on compression the responses I'm logging aren't compressed whereas what I see in Fiddler is.

So my question - is it possible to log the compressed content or is this happening in a subsequent step my module can't hook in to?

For the record, I've also tried handling the PreSendRequestContent event and the response is still uncompressed.


回答1:


Hi although I cannot answer your questions directly I have had cause to do similar things in the past and have found the following resource extremely helpful and enlightening. In the end I managed to acheive what I needed for raw soap headers by configuring the System.Diagnostics node within web config and create a trace log of traffic. I understand that your needs may be more granular than that however I believe this resource may still help.

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

Of particular interest may be the message log configuration and viewing message logs links from the one above.



来源:https://stackoverflow.com/questions/11084459/logging-raw-and-compressed-http-responses-in-asp-net-iis7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!