MVC5应用程序中的计时请求

时间:2019-07-09 07:47:05

标签: c# .net asp.net-mvc asp.net-web-api asp.net-mvc-5.2

我有一个MVC 5 WebAPI应用程序,尝试非常近似地估计每个请求的时间,以查看哪个请求花费了多少时间,并最终优化了最昂贵的请求。

我的代码如下

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

但是输出基本上是相同的:

  

MyServices请求/ MyServices / ZoneBlock / GetZoneBlock   contentTypeId = 18&blockName = sliderhautauto_slide2已使用   -00:00:00.0010000

对于任何请求,“时间”都相同(-00:00:00.0010000),这对我来说似乎很奇怪...

PS。
我的应用程序基于.NET 4.5构建,因此不幸的是,像MiniProfiler(。NET 4.6.1分钟)之类的工具无法在我的情况下使用...

1 个答案:

答案 0 :(得分:-1)

不要自己写, 该时间将已经在应用程序的日志中(至少在使用标准日志记录设置时)。如果您需要它更易于访问,我喜欢使用miniprofiler。

相关问题