如何在global.asax中捕获响应大小(内容长度)?

时间:2017-05-30 02:01:32

标签: c# logging httpresponse global-asax

我正在尝试使用以下代码捕获global.asax中的http响应内容长度或响应大小。

选项1

var buffer = new MemoryStream();
this.LogInfoForOperations(String.Format("Response Size: {0}", buffer.Length));

选项2

HttpApplication myHttpApplication = (HttpApplication)sender;
this.LogInfoForOperations(String.Format("Response Size: {0}", myHttpApplication.Context.Response.OutputStream.Length));

错误/输出:

[Error] An unhandled exception has been caught by Global.asax Application_Error handler...The operation has timed out
   at System.Net.HttpWebRequest.GetResponse()
   at AppName.Web.MvcApplication.Application_EndRequest(Object sender, EventArgs e)


[Information] Response Size: System.Web.HttpResponse

出于某种原因,我提出的那两个代码没有抛出响应的大小。在获得响应大小时,能帮我弄清楚request.totalbytes的等价值吗?我可以使用Request.TotalBytes但不是http响应来获取请求大小。

***更新31052017

我现在有了一个正常工作的代码但是我总是得到9个响应大小并且它看起来不准确。您有什么建议可以让我改进下面的代码吗?我需要这个来确定哪个事务消耗了大量的请求和响应,除了我从新文件中获取的时间。

int responseLength = HttpContext.Current.Response.ContentType.Length;
this.LogInfoForOperations(String.Format("Response Size: {0}", responseLength));
NewRelic.Api.Agent.NewRelic.AddCustomParameter("responseSize", responseLength);

0 个答案:

没有答案