Once you have set up Application Insights it will gather telemetry and publish it to Azure. This is great but you may want to ignore telemetry coming from specific environments, say your dev or staging environments. One possible solution is to set TelemetryConfiguration.Active.DisableTelemetry to false. Unfortunately, this will not completely disable all communications and features like Live Metrics will continue to function. The following is a screenshot of an app with disabled telemetry but obviously data is still being sent.
This is not ideal if you do not want information being sent from development machines or staging environments.
Your best option at the moment is to remove the instrumentation key. When no instrumentation key is present, no data will be sent. Of course, simply removing the key is not a flexible approach. We need the ability to remove the key for environments that should not send any data and, conversely, include the key for environments that should.
By default, the instrumentation key resides in ApplicationInsights.config from where it is read when your application starts. Having the key in this XML file is not very flexible if conditionally changing/removing the key is a requirement. Yes, there are tools and framework features that allow you to transform random XML files (we have written about that), but bringing additional tools for simply disabling a feature does not seem justifiable.
The solution that I am proposing involves the following simple steps:
- Set the instrumentation key in code
- Modify or remove the key based on the environment
Setting the key in code
Setting the instrumentation key in code allows for greater control on how the key is read and set, thus making it easy to turn Application Insights on and off.
For instance, if you are using Application Insights in a ASP.NET web app, the best place to read the key from is web.config. We can easily setup a web.config transformation that includes your instrumentation key for Release builds only.
Here is how you can set the key on application start:
As you can tell, setting the key manually is a piece of cake - just set the TelemetryConfiguration.Active.InstrumentationKey property.
Modifying the key based on the environment
The approach that works best in general, involves setting the default instrumentation key to empty string. This is basically an opt-in approach where you must explicitly enable Application Insights for specific environments - your production environment, for example. All other environments like your dev ones will not send data by default.
As I have mentioned, you can use web.config transformations to control which environments have Application Insights enabled but if you are deploying to Azure, using Application settings is the way to go.
Using Application Settings on Azure
If your web app is running on Azure, ideally, you should use application settings to control which of your environments sends data to Application Insights. Check out this article if you are new to using application settings
To enable Application Insights for your production web app, navigate to the Application settings tab of your web app. Add a new app setting using the same name that is used in your code where the key is read. In my case this is AppInsights:InstrumentationKey. Set your instrumentation key as the value and make sure that this setting is designated as slot setting.
In the end, the app settings should look like this:
Once you save the settings, your production app will retrieve the key and start sending data to Application Insights.
The technique outlined here can easily be adapted to use different Application Insights instrumentation keys for different environments. More information is provided here.
Give feedback to the Application Insights team
Currently there is a debate on Github on how and if turning off Application Insights completely should be supported out of the box. Feel free to comment and provide feedback to the dev team.