4

Here's an easy one for you:

I'm currently logging request duration via an HttpModule and I'd like to find out the number of bytes each page is as well.

HttpContext.Current.Response.OutputStream.Length throws a NotSupportedException.

What's an easy way to do this?

e-sushi
  • 13,786
  • 10
  • 38
  • 57
Ben Scheirman
  • 40,531
  • 21
  • 102
  • 137

1 Answers1

3

I have an HttpModule that implements a stream rewriter. It derives from the Stream class. In my HttpModule I have the following code:

void app_PreRequestHandlerExecute(object sender, EventArgs e)
{
    HttpResponse response = HttpContext.Current.Response;
    response.Filter = new MyRewriterStream(response.Filter);
}

In the stream class I have the following code that overrides the default Write method:

public override void Write(byte[] buffer, int offset, int count)
{
     string outStr;
     outStr = UTF8Encoding.UTF8.GetString(buffer, offset, count);
     //Do useful stuff and write back to the stream
}

You can just take the length of the string at the second point

Jason Z
  • 13,122
  • 15
  • 50
  • 62