简体   繁体   English

MVC5 应用程序中的计时请求

[英]Timing requests in MVC5 application

I have an MVC 5 WebAPI application and try to estimate, very approximately, the timing of each request, to see what request how many time it took and eventually optimize the most expensive ones.我有一个 MVC 5 WebAPI 应用程序,并尝试非常近似地估计每个请求的时间,以查看哪个请求花费了多少时间并最终优化最昂贵的请求。

my code is the following我的代码如下

static public int time;
private DateTime start;

protected void Application_BeginRequest(Object sender, EventArgs e)
{
    start = DateTime.Now;
}

protected void Application_EndRequest(Object sender, EventArgs e)
{
    time = start.CompareTo(DateTime.Now);
    var request = ((System.Web.HttpApplication)sender).Request;
    System.Diagnostics.Debug.WriteLine($">> MyServices request {request.Path} {request.QueryString} took {TimeSpan.FromMilliseconds(time)}");            
}

However the output is mainly the same:然而,输出主要是相同的:

MyServices request /MyServices/ZoneBlock/GetZoneBlock contentTypeId=18&blockName=sliderhautauto_slide2 took -00:00:00.0010000 MyServices 请求 /MyServices/ZoneBlock/GetZoneBlock contentTypeId=18&blockName=sliderhautauto_slide2 花了-00:00:00.0010000

the "time" is the same (-00:00:00.0010000) for any request, that seem strange to me...对于任何请求,“时间”都是相同的(-00:00:00.0010000),这对我来说似乎很奇怪......

PS.附注。
My application is built on .NET 4.5, so the tools like MiniProfiler (.NET 4.6.1 min) unfortunately will not work for my case...我的应用程序是基于 .NET 4.5 构建的,因此很遗憾, MiniProfiler (.NET 4.6.1 分钟)之类的工具不适用于我的情况...

Don't write that yourself, the time will already be in the log of the application (at least when standard logging settings are used).不要自己写,时间已经在应用程序的日志中(至少在使用标准日志设置时)。 If you need it more accessible, I like to use miniprofiler.如果您需要更易于访问,我喜欢使用 miniprofiler。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM