如何向MiniProfiler添加启动时间?

时间:2015-06-26 22:09:04

标签: c# mvc-mini-profiler

我想使用MiniProfiler计算Application_OnStart。到目前为止,我一直没有成功。我试图创建一个Timing并将其添加到root的子节点,但它不会收集我想要的所有数据。它还会从整体配置文件时间中减去启动时间,因此最终会显示负的持续时间。目前我唯一的解决方案是使用秒表计时并将其添加到客户端计时。这将从第一个请求中删除客户端计时,并添加应用程序启动持续时间。但我没有获得自定义时序等。此外,因为我使用AddProfilerResults方法在我的MVC配置文件中组合我的API配置文件(对于MVC UI,它们包含在步骤中),第一个MVC上API的启动时间请求不包括在内,而且基本上丢失了。

            MiniProfiler.Current.ClientTimings = new ClientTimings
            {
                Timings = new List<ClientTimings.ClientTiming>
                {
                    new ClientTimings.ClientTiming
                    {
                        Duration = startTime,
                        Id = Guid.NewGuid(),
                        MiniProfilerId = Guid.NewGuid(),
                        Name = "Application_Start",
                        Start = 0
                    }
                }
            };

1 个答案:

答案 0 :(得分:5)

我们在Stack Overflow上执行此操作的方法是在启动方法中创建MiniProfiler,向其添加步骤,然后将其呈现为静态字符串:

public static string ApplicationStartTimings { get; private set; }

protected void Application_Start(object sender, EventArgs e)
{
    // init MiniProfiler.Settings here
    // ...
    var prof = new MiniProfiler("Application_Start");

    using (prof.Step("Register routes"))
    {
        RegisterRoutes(RouteTable.Routes);
    }

    // ... more config and steps

    try
    {
        ApplicationStartTimings = prof.Render().ToString();
    }
    catch { }
}

然后您可以有一条路线来显示这些时间。这很糟糕,但是完成了工作。

编辑 - 我添加了branch to my MiniProfiler fork which demonstrates这个工作;这是主索引页面上的输出:

enter image description here

相关问题