为了在多个环境中支持Application Insights,我们以编程方式设置检测 key ,如 this post 中的建议。 .
我们已经离开<InstrumentationKey>
ApplicationInsights.config 中为空,而是在 web.config 中添加了应用程序设置:
<add key="ApplicationInsightsInstrumentationKey" value="1234-5678-9xxx" />
在Application_Start中,我们执行以下操作来设置检测 key :
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 必须先初始化才能发送遥测数据”。
谷歌搜索此异常消息没有产生任何结果,但我想在跟踪事件之前还需要一些额外的东西?
最佳答案
删除 <InstrumentationKey />
ApplicationInsights.config 中的元素似乎可以解决问题。
我做了完全相同的事情,在 Web 应用程序的 Application_Start() 中设置 iKey,然后在控制台应用程序中 new() 建立一个 TelemetryClient 。我的 ApplicationInsights.config 中没有任何元素,甚至没有空白元素。我在该配置文件中保留了一条注释,表示 key 是以编程方式设置的。
关于c# - 以编程方式设置 Application Insights 检测键会引发错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31471176/