【问题标题】:Programmatically setting Application Insights instrumentation key throws error以编程方式设置 Application Insights 检测密钥会引发错误
【发布时间】:2015-07-17 08:11:44
【问题描述】:

为了在多种环境中支持 Application Insights,我们按照this post 中的建议以编程方式设置Instrumentation Key

我们将 ApplicationInsights.config 中的 <InstrumentationKey> 留空,而是在 web.config 中添加了一个应用程序设置:

<add key="ApplicationInsightsInstrumentationKey" value="1234-5678-9xxx" />

Application_Start 中,我们执行以下操作来设置检测密钥:

var instrumentationKey = ConfigurationManager.AppSettings["ApplicationInsightsInstrumentationKey"];

if (string.IsNullOrWhiteSpace(instrumentationKey))
{
    throw new ConfigurationErrorsException("Missing app setting 'ApplicationInsightsInstrumentationKey' used for Application Insights");
}

TelemetryConfiguration.Active.InstrumentationKey = instrumentationKey;

new TelemetryClient().TrackEvent("Application started");

但是,这会产生一个异常,提示“必须先初始化 TelemetryChannel,然后才能发送遥测数据”。

谷歌搜索此异常消息不会产生任何结果,但我想在跟踪事件之前还需要一些额外的东西?

【问题讨论】:

标签: c# azure azure-web-app-service azure-application-insights


【解决方案1】:

从 ApplicationInsights.config 中删除 &lt;InstrumentationKey /&gt; 元素似乎可以解决问题。

我已经完成了完全相同的操作,在 Application_Start() 中为 Web 应用程序设置 iKey,然后在控制台应用程序中使用 new() 设置 TelemetryClient。我的 ApplicationInsights.config 中确实 没有 有任何元素,甚至没有一个空白元素。我在该配置文件中保留了一条注释,说明正在以编程方式设置密钥。

【讨论】:

  • 是的,看来必须将元素完全删除,这有点奇怪?
  • 这也是我的经验。
猜你喜欢
  • 2020-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-08
  • 1970-01-01
  • 2014-06-29
  • 2014-02-28
相关资源
最近更新 更多