Application Insight (Azure) 中的计时事件

Ton*_*llo 1 azure azure-application-insights azure-web-app-service

我一直在寻找一种方法来计时事件并将它们绘制在 Azure 上。寻找事件较慢的热点以进行进一步分析。

我目前可以执行以下操作,例如:

var p = new Dictionary<string, string> {{ "StartTime", startTime.Value.ToString("g") }, { "EndTime", endTime.Value.ToString("g") }};
var m = new Dictionary<string, double> {{ "ElapsedSeconds", (endTime.Value - startTime.Value).TotalSeconds }};

ai.TrackEvent(eventName, p, m);
Run Code Online (Sandbox Code Playgroud)

这将使我能够一次查看一个事件并知道它花费了多长时间。但是没有简单的方法来查看它的图表。但是,我注意到他的 javascript 库有一个 startTrackEvent 和 stopTrackEvent(AI 文档),这看起来很理想。

有没有人看到一种内置方式或现有方式来跟踪定时服务器事件?

Joh*_*ner 6

然后你可以像 Ketan's answer 这样的东西,用一次性用品包起来,比如:

internal class TimedEvent : IDisposable
{
    private string name;
    private Dictionary<string,string> properties;
    private Stopwatch timer = Stopwatch.StartNew();

    public TimedEvent(string name, Dictionary<string,string> properties = null)
    {
        this.name = name;
        this.properties = properties;
    }

    public void Dispose()
    {
        timer.Stop();
        YourTelemetryClientHere.TrackEvent(this.name, this.properties, 
            new Dictionary<string, double> { { "duration", timer.ElapsedMilliseconds } });
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在你的代码中你可以做类似的事情

using (new TimedEvent("myEvent"))
{
     // do something that takes time
}
Run Code Online (Sandbox Code Playgroud)