18

如何最好地捕获由 aspx 页面呈现的 HTML(在我的实例中,用于日志记录)?

我不想使用 Response.Write 写回页面,因为它会打乱我的网站布局。

使用 Response.OutputStream 或 Response.Output 的流会导致 ArgumentException({System.ArgumentException: Stream was not readable.)

4

3 回答 3

25

好问题,我不得不尝试看看是否可以创建一个 HttpModule 来执行您所描述的操作。

我没有任何运气尝试从响应流中读取,但是使用 ResponseFilter 给了我一种捕获内容的方法。

以下代码似乎工作得很好,我想也许您可以使用该代码作为基础。但请记住,这只是我快速拼凑的东西,还没有经过任何测试。因此,如果没有适当的审查/测试等,请勿在任何生产环境中使用它。随意评论它虽然;)

public class ResponseLoggerModule : IHttpModule
{
    private class ResponseCaptureStream : Stream
    {
        private readonly Stream _streamToCapture;
        private readonly Encoding _responseEncoding;

        private string _streamContent;
        public string StreamContent
        {
            get { return _streamContent; }
            private set
            {
                _streamContent = value;
            }
        }

        public ResponseCaptureStream(Stream streamToCapture, Encoding responseEncoding)
        {
            _responseEncoding = responseEncoding;
            _streamToCapture = streamToCapture;

        }

        public override bool CanRead
        {
            get { return _streamToCapture.CanRead; }
        }

        public override bool CanSeek
        {
            get { return _streamToCapture.CanSeek; }
        }

        public override bool CanWrite
        {
            get { return _streamToCapture.CanWrite; }
        }

        public override void Flush()
        {
            _streamToCapture.Flush();
        }

        public override long Length
        {
            get { return _streamToCapture.Length; }
        }

        public override long Position
        {
            get
            {
                return _streamToCapture.Position;
            }
            set
            {
                _streamToCapture.Position = value;
            }
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            return _streamToCapture.Read(buffer, offset, count);
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            return _streamToCapture.Seek(offset, origin);
        }

        public override void SetLength(long value)
        {
            _streamToCapture.SetLength(value);
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            _streamContent += _responseEncoding.GetString(buffer);
            _streamToCapture.Write(buffer, offset, count);
        }

        public override void Close()
        {
            _streamToCapture.Close();
            base.Close();
        }
    }

    #region IHttpModule Members

    private HttpApplication _context;
    public void Dispose()
    {

    }

    public void Init(HttpApplication context)
    {
        _context = context;

        context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
        context.PreSendRequestContent += new EventHandler(context_PreSendRequestContent);
    }

    void context_PreRequestHandlerExecute(object sender, EventArgs e)
    {
        _context.Response.Filter = new ResponseCaptureStream(_context.Response.Filter, _context.Response.ContentEncoding);
    }

    void context_PreSendRequestContent(object sender, EventArgs e)
    {
        ResponseCaptureStream filter = _context.Response.Filter as ResponseCaptureStream;

        if (filter != null)
        {
            string responseText = filter.StreamContent;

            // Logging logic here
        }
    }

    #endregion
}
于 2008-12-22T16:21:59.853 回答
4

许多负载测试器允许您记录生成的 HTTP 响应,但请记住,对于 ASP.NET,这些可能是一些非常大的日志文件。

编辑:根据 Tom Jelen 的代码,Response.Filter 旨在提供这种监督,否则 Response.Outputstream 是不可读的。

编辑 2:对于页面而不是 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() */

  //...

}

并在您的 Page_Load 中(或在您的回复被写之前)

Response.Filter = new ObserverStream(Response.Filter);
于 2008-12-22T15:23:16.493 回答
1

向您自己的服务器发出服务器端 XMLHTTP 请求的一种方法。获取结果并将其保存到文件或数据库中。

或者,您可以在客户端上使用 AJAX,获取结果并将其 POST 回服务器。

于 2008-12-22T15:35:11.353 回答