PostRequestHandlerExecute中的响应长度

时间:2010-06-24 21:22:56

标签: asp.net response

我想确切地知道响应发送给用户的确切时间是多久,以便进行日志记录。有没有办法从asp.net中的HttpModule(在PostRequestHandlerExecute事件中)执行此操作。

2 个答案:

答案 0 :(得分:1)

不幸的是,HttpResponse.OutputStream只写,所以这不是很简单 - 任何查看输出流的Length属性的尝试都会引发异常。

我见过的唯一解决方案是将过滤器应用于Response对象,以便过滤器可以计算字节数。

快速谷歌搜索landed me here,这似乎与我记得的实施很接近。

答案 1 :(得分:1)

希望得到帮助

context.PostRequestHandlerExecute += delegate(object sender, EventArgs e)
{
    HttpContext httpContext = ((HttpApplication)sender).Context;
    HttpResponse response = httpContext.Response;
    // Don't interfere with non-HTML responses
    if (response.ContentType == "text/html")
    {
        response.Filter = new MyRewriterStream(response.Filter);
    }
};

MyRewriterStream类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;

namespace ExecutionTime
{
    public class MyRewriterStream:Stream
    {
        #region "Propiedades"

        private Stream _sink;

        #endregion
        public MyRewriterStream(System.IO.Stream stream)
        {

            _sink = stream;
        }
        public override void Write(byte[] buffer, int offset, int count)
        {
            string outStr;
            outStr = UTF8Encoding.UTF8.GetString(buffer, offset, count);
            strPageSize = strPageSize + outStr;
            StringBuilder sb = new StringBuilder(outStr);

            if (sb.ToString().LastIndexOf("</html>") > 0)
            {
                 string HtmlResponse = "";//HERE PUT YOUR NEW HTML RESPONSE
                 sb.AppendLine(HtmlResponse );
                 byteArray = Encoding.ASCII.GetBytes(sb.ToString());
                 _sink.Write(byteArray, 0, byteArray.Length);
            }
            else
            {
                _sink.Write(buffer, offset, count);
            }

        }

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


        #region Properites

        public override bool CanRead
        {
            get { return true; }
        }

        public override bool CanSeek
        {
            get { return true; }
        }

        public override bool CanWrite
        {
            get { return true; }
        }

        //public override void Flush()
        //{
        //    _sink.Flush();
        //}

        public override long Length
        {
            get { return 0; }
        }

        private long _position;
        public override long Position
        {
            get { return _position; }
            set { _position = value; }
        }

        #endregion

        #region Methods

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

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

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

        public override void Close()
        {
            _sink.Close();
        }
        #endregion
    }
}