1

我一直在寻找一种方法来对事件进行计时并将它们绘制在 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

4 回答 4

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

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

using (new TimedEvent("myEvent"))
{
     // do something that takes time
}
于 2015-01-30T01:38:35.537 回答
2

找了一会儿,我想你要找的是TrackDependency方法。

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

于 2020-06-02T21:04:09.087 回答
1

我们已经在 SDK 中提供了随事件发送自定义指标的功能,但目前这些指标并未显示在 UI 中。几周后,您将能够在 Application Insights 中看到自定义指标。因此,您应该将 elapseSeconds 作为事件的指标。 IDictionary<string, double> mDictionary = new Dictionary<string, double>(); mDictionary.Add("ElaspsedSeconds", m); ai.TrackEvent(eventName, mDictionary); 几周后,您将绘制这些指标与您在 Application Insights 中看到的任何其他指标一样的图表。

于 2015-01-29T06:20:30.023 回答
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.
于 2018-04-16T01:12:06.810 回答