How can I disable application insights automatically when using a debug configuration and enable it only on release?
Is it possible to do this without creating another instrumentation key only for debug?
As explained in the question not deploying or deploying an ApplicationInsights.config without
<instrumentationkey>key</instrumentationkey> block events from being generated.
You can then put the instrumentation key in code (only on release in my case)
#if !DEBUG WindowsAppInitializer.InitializeAsync("instrumentation key"); #endif
TelemetryClient created after this call will have the correct key and will track events so you don't have to change the code in all places.
Not calling the method above or leaving the parameter empty will block events because there isn't a key configured.
ApplicationInsights.config file overrides any code that set the instrumentation key, removing the
<instrumentationkey>key</instrumentationkey> inside it will let you use code to configure the key.
If you remove the file completely it doesn't work.
Here is the confirm: "If you want to set the key dynamically - for example if you want to send results from your application to different resources - you can omit the key from the configuration file, and set it in code instead."