C# 以编程方式设置Application Insights instrumentation键会引发错误
为了在多个环境中支持应用程序洞察,我们以编程方式设置Instrumentation键,如中所述 我们将ApplicationInsights.config中的C# 以编程方式设置Application Insights instrumentation键会引发错误,c#,azure,azure-web-app-service,azure-application-insights,C#,Azure,Azure Web App Service,Azure Application Insights,为了在多个环境中支持应用程序洞察,我们以编程方式设置Instrumentation键,如中所述 我们将ApplicationInsights.config中的保留为空,并在web.config中添加了一个应用程序设置: <add key="ApplicationInsightsInstrumentationKey" value="1234-5678-9xxx" /> 但是,这会产生一个异常,说明“遥测通道必须先初始化,然后才能发送遥测数据” 用谷歌搜索此异常消息不会产生任何结果,但
保留为空,并在web.config中添加了一个应用程序设置:
<add key="ApplicationInsightsInstrumentationKey" value="1234-5678-9xxx" />
但是,这会产生一个异常,说明“遥测通道必须先初始化,然后才能发送遥测数据”
用谷歌搜索此异常消息不会产生任何结果,但我想在跟踪事件之前还需要一些额外的东西?从ApplicationInsights.config中删除
元素似乎可以解决问题
我做了完全相同的事情,为web应用程序设置了应用程序中的iKey\u Start(),在我创建新的()之前,在控制台应用程序中设置了遥测客户端。我的ApplicationInsights.config中没有任何元素,甚至没有空白元素。我在配置文件中保留了一条注释,说明密钥是通过编程方式设置的。这可能是因为ApplicationInsights.config中的
元素为空。完全删除了元素,现在似乎有效。这篇博文帮助我澄清了一些细节:是的,似乎元素必须完全删除,这有点奇怪?这也是我的经历。
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");