Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以编程方式设置Application Insights instrumentation键会引发错误_C#_Azure_Azure Web App Service_Azure Application Insights - Fatal编程技术网

C# 以编程方式设置Application Insights instrumentation键会引发错误

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" /> 但是,这会产生一个异常,说明“遥测通道必须先初始化,然后才能发送遥测数据” 用谷歌搜索此异常消息不会产生任何结果,但

为了在多个环境中支持应用程序洞察,我们以编程方式设置Instrumentation键,如中所述

我们将ApplicationInsights.config中的
保留为空,并在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");