Application Insight中的计时事件(Azure)

时间:2015-01-27 04:37:40

标签: azure azure-web-sites azure-application-insights

我一直在寻找一种时间事件的方法,并将它们绘制在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);

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

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

4 个答案:

答案 0 :(得分:4)

然后你可以拿出Ketan的答案,并用一次性包装,如:

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

然后在您的代码中,您可以执行类似

的操作
using (new TimedEvent("myEvent"))
{
     // do something that takes time
}

答案 1 :(得分:0)

我们已经在SDK中使用功能发送自定义指标以及事件,但目前这些指标未显示在UI中。几周后,您将能够在Application Insights中看到自定义指标。因此,您应该将elapseSeconds作为事件的度量标准。       IDictionary<string, double> mDictionary = new Dictionary<string, double>(); mDictionary.Add("ElaspsedSeconds", m); ai.TrackEvent(eventName, mDictionary); 几周之后,您就可以将这些指标列为您在Application Insights中看到的任何其他指标。

答案 2 :(得分:0)

AI有一项功能,使用TrackRequest提供解决方案:

//Establish an operation context and associated telemetry item:
using (var operation = telemetryClient.StartOperation<RequestTelemetry>("operationName"))
{
    // Telemetry sent in here will use the same operation ID.
    ...
    telemetryClient.TrackTrace(...); // or other Track* calls
    ...
    // Set properties of containing telemetry item--for example:
    operation.Telemetry.ResponseCode = "200";

    // Optional: explicitly send telemetry item:
    telemetryClient.StopOperation(operation);

} // When operation is disposed, telemetry item is sent.

答案 3 :(得分:0)

寻找了一段时间之后,我认为您正在寻找的是TrackDependency方法。

它在依赖关系图上很好地显示了程序在代码的每个部分上花费了多长时间。

相关问题