【问题标题】:Application Insights Takes time to send eventApplication Insights 发送事件需要时间
【发布时间】:2017-05-18 21:55:47
【问题描述】:
我开发了一个 UWP 应用程序,我在其中使用 Application Insights 跟踪应用程序的页面视图和自定义事件。我还在应用程序关闭事件期间添加了自定义事件,但应用程序关闭事件没有被跟踪正在跟踪其他自定义事件和页面浏览量。经分析,我们发现 AI 需要一些时间来发送事件。有什么办法可以减少这个时间?
【问题讨论】:
标签:
uwp
azure-application-insights
【解决方案1】:
Application Insights SDK 中的 flush 推荐努力刷新缓冲区中剩余的遥测数据,但不保证交付。
确保发送最后一个事件的一种方法是在结束进程之前添加一个简单的thread.sleep 调用。但是,如果您想确保所有事件都以同步方式发送,您可以实现自己的遥测通道,在返回之前发送事件。
您可以看到the full example here,但一个简单的同步遥测通道将如下所示:
class SyncTelemetryChannel : ITelemetryChannel
{
private Uri endpoint = new Uri("https://dc.services.visualstudio.com/v2/track");
public bool? DeveloperMode { get; set; }
public string EndpointAddress { get; set; }
public void Dispose() { }
public void Flush() { }
public void Send(ITelemetry item)
{
byte[] json = JsonSerializer.Serialize(new List<ITelemetry>() { item }, true);
Transmission transimission = new Transmission(endpoint, json, "application/x-json-stream", JsonSerializer.CompressionType);
var t = transimission.SendAsync();
t.Wait();
}
}