0

我在使用以下代码记录到 Application Insights 时遇到问题:

var configuration = new TelemetryConfiguration();
            configuration.InstrumentationKey = "KEY";
            var client = new TelemetryClient(configuration);
            client.TrackEvent($"TEST EVENT", properties: new Dictionary<string, string>() { { $"TEST Property", $"TEST Value".ToString() } });

每当我使用下面的代码时,遥测数据都会毫无问题地记录下来,但是,由于这不是异步调用,我们永远不会得到IsCancellationRequestedasFALSE并且这会进入无限循环。

CancellationTokenSource tokenSource = new CancellationTokenSource();
            CancellationToken token = tokenSource.Token;
            var configuration = new TelemetryConfiguration();
            configuration.InstrumentationKey = "KEY";
            var client = new TelemetryClient(configuration);
            while (!token.IsCancellationRequested)
            {
                client.TrackEvent($"TEST EVENT", properties: new Dictionary<string, string>() { { $"TEST Property", $"TEST Value".ToString() } });
            }

有人可以帮我确定我是否在这里遗漏了什么吗?

-沙拉布

4

1 回答 1

0

有几个可能的原因:

  • 您正在创建新TelemetryConfiguration对象,它默认为没有重试/没有本地存储的简单遥测通道,因此网络问题将导致事件丢失。您可以使用TelemetryConfiguration.Active从 ApplicationInsights.config 初始化的内容,并将ServerTelemetryChannel与重试和本地存储一起使用。
  • 执行该方法后应用程序可能会立即退出,没有机会填充遥测缓冲区并发送遥测。如果是这种情况,您可以使用Flush()除非您实现了同步通道Flush(),否则单独无济于事,因此您可能还需要添加 a 。Sleep()
于 2018-05-03T21:52:26.040 回答