c# - 以编程方式设置 Application Insights 检测键会引发错误

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

为了在多个环境中支持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/

相关文章:

c# - 如何有条件地启动一个 Action ?

Azure 数据工厂 V2 : Optional parameters

Azure 网站 : NetworkInterface. GetIsNetworkAvailable() 始终返回 false

linux - Azure Linux 应用服务 : Installing packages after deploy from Devops pipeline

c# - Xamarin获取wifi信号强度

c# - 在 C# 中创建新消息队列 (MSMQ) 时设置默认权限

azure - 配置 Azure 负载均衡器和 NAT 规则

c# - 启用 https 时,应用程序设置中的连接字符串不起作用 : Azure Web App

c# - 将显式强制转换作为 ref 参数传递 (C#)

wcf - 在 azure 上托管 WCF 服务