Alberto Rivelli Alberto Rivelli - 1 year ago 472
C# Question

Disable application insights in debug

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?

I have

statements scattered all over the code, enclosing them inside a debug preprocessor check is not an ideal solution.

My current solution is to set the
Build Action
of the
file to
so that it's not copied to the project's output directory, but this isn't a process that can be automated based on the active build configuration.

There is a Developer Mode but needs to be changed manually (if it was possible to conditionally set the config file, emptying the instrumentationkey solved problem as well)



Answer Source

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");

Every 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.

Basically the 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."


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download