Measuring HttpResponse stream size

Published Sat, Sep 18 2004 7:02 | girishb

Here is a way to measure the HttpResponse.OutputStream size. Following is the sources for global.asax file. This is written over the course of half-hour so bear with me.

I have marked the parts that are interesting in bold. If you are interested in applying a filter, this can be a template for you.

 

<%@Import Namespace="System.IO" %>

<%@ Import namespace="System.Text" %>

 

<Script language="C#" runat="server">

 

void Application_OnBeginRequest(Object o, EventArgs e) {

        Response.Filter = new CounterClass(Response.Filter);

}

void Application_OnEndRequest(Object o, EventArgs e) {

         using (StreamWriter st = File.AppendText(@"C:\temp\Myevents.txt"))

{

st.WriteLine("{0}",Response.Filter.Length);

st.Flush();

}

}

 

public class CounterClass : Stream {

        private Stream _sink;

        private int _count;

 

public CounterClass(Stream sink)

{

_sink = sink;

}

 

public override bool CanRead

{

get { return true; }

}

 

public override bool CanSeek

{

get { return true; }

}

 

public override bool CanWrite

{

get { return true; }

}

 

public override long Length

{

get { return _count; }

}

 

public override long Position

{

get { return _sink.Position; }

set { _sink.Position=value; }

}

 

public override int Read(byte[] buffer, int offset, int count)

{

        throw new NotSupportedException();

}

 

public override long Seek(long offset, System.IO.SeekOrigin direction)

{

        return _sink.Seek(offset,direction);

}

 

public override void SetLength(long length)

{

        _sink.SetLength(length);

}

 

public override void Close()

{

_sink.Close();

}

 

public override void Flush()

{

_sink.Flush();

}

 

public override void Write(byte[] buffer, int offset, int count)

{

        _count += count;

         _sink.Write(buffer, offset, count);

}

 

}

</script>

 

Next question is to figure out the size of headers.