Capturing HTML generated from ASP.NET

后端 未结 2 1689
無奈伤痛
無奈伤痛 2020-12-08 21:31

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

2条回答
  •  温柔的废话
    2020-12-08 21:48

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

提交回复
热议问题