How do I best capture the HTML (in my instance, for logging) rendered by an aspx-page?
I dont want to have to write back to the page using Response.Write, since it m
Many load testers will allow you to log the HTTP responses generated, but bear in mind with ASP.NET those could be some very large log-files.
Edit: Response.Filter as per Tom Jelen's code is designed to give this kind of oversight and Response.Outputstream is otherwise unreadable.
Edit 2: For a page rather than a HTTPModule
public class ObserverStream : Stream
{
private byte[] buffer = null;
private Stream observed = null;
public ObserverStream (Stream s)
{
this.observed = s;
}
/* important method to extend #1 : capturing the data */
public override void Write(byte[] buffer, int offset, int count)
{
this.observed.Write(buffer, offset, count);
this.buffer = buffer; //captured!
}
/* important method to extend #2 : doing something with the data */
public override void Close()
{
//this.buffer available for logging here!
this.observed.Close();
}
/* override all the other Stream methods/props with this.observed.method() */
//...
}
and in your Page_Load (or before your response is written anyway)
Response.Filter = new ObserverStream(Response.Filter);