Programmatically configuring Application Insights toolkit key generates an error

In order to support Application Insights in several environments, we programmatically install the Tool Key, as recommended in this post .

We left the <InstrumentationKey> in ApplicationInsights.config empty and instead added the application settings to web.config :

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

At Application_Start, we do the following to set the toolkit 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"); 

However, this raises an exception: "The TelemetryChannel must be initialized before it can send telemetry .

Inclusion of this exception message yields nothing, but I assume there is something extra needed to track events?

+6
source share
1 answer

Removing the <InstrumentationKey /> element from ApplicationInsights.config seems like a trick.

I did the same thing by setting iKey in Application_Start () for the web application and before I again () add TelemetryClient to the console applications. I do not have an element in my ApplicationInsights.config, not even empty. I save the comment in this configuration file, which says that the key is installed programmatically.

+8
source

All Articles