【发布时间】:2018-10-05 19:27:22
【问题描述】:
我有一个实现IDisposable 的小型分析类,它在构造函数中启动秒表并在Dispose() 中停止它,并使用TelemetryClient.TrackTrace 将经过的时间(以毫秒为单位)记录到Azure。它工作正常,但我希望它出现在 Azure 门户的 Application Insights 页面的时间轴上。如果我使用TelemetryClient.TrackDependency,我可以让它出现,但由于它不是依赖项,我不想使用它。有没有办法让它出现在时间线上?我尝试将经过的时间添加为一个名为 duration 的属性,但这不起作用。
我不知道它是否有帮助,但分析器的简化版本看起来像这样:
public class Profiler : IDisposable
{
private readonly Stopwatch _stopwatch;
private readonly ILogger _logger;
private readonly string _name;
private readonly DateTimeOffset _timestamp;
public Profiler(string name)
{
_logger = LogFactory.GetLogger();
_stopwatch = Stopwatch.StartNew();
_timestamp = DateTimeOffset.UtcNow;
_name = name;
}
public static Profiler Step(string name)
{
return new Profiler(name);
}
public void Dispose()
{
var telemetryClient = new TelemetryClient();
_stopwatch.Stop();
var message = $"Step - {_name} {_stopwatch.ElapsedMilliseconds} ms";
var traceTelemetry = new TraceTelemetry(message, SeverityLevel.Verbose)
{
Timestamp = _timestamp
};
traceTelemetry.Properties.Add("Elapsed Milliseconds", $"{_stopwatch.ElapsedMilliseconds}");
telemetryClient.TrackTrace(traceTelemetry);
}
}
【问题讨论】:
标签: c# asp.net-web-api azure-application-insights