Disabling the Application Insights during development / debug mode could be a common requirement. Getting Started with Application Insights is very easy and it helps us to monitor the applications health in real time. You can track and visualize your applications availability, performance issues, users session and diagnose crashes faster then ever. How to disable it when you don’t want to track the telemetry information ? Consider some scenarios like, you are debugging an application or enhancing top of an existing application where Application Insights is already configured . In such cases, of course you don’t want the application insights to log your application telemetry.
I came across this question “Disable application insights in debug” couple of weeks back and shared by view. thought to share it as a post so that it may help others.
TelemetryConfiguration
class supports all the telemetry configuration related APIs that reads the information from ApplicationInsights.config
file. You will find this class under Microsoft.ApplicationInsights.Extensibility
namespaces.
TelemetryConfiguration.Active
Property, returns the current active instance of TelemetryConfiguration
during the execution of application.
Once you have the instance of Active TelemetryConfiguration
, you can disable it by settings the DisableTelemetry
as true.
Here is the code snippet to disable the Application Insights
TelemetryConfiguration.Active.DisableTelemetry = true;
This will restrict the application to send the telemetry information .
Consider you have following code, that sends a custom telemetry event on each button click.
private void button_Click(object sender, RoutedEventArgs e) { TelemetryClient telemetry = new TelemetryClient(); telemetry.TrackEvent("Started Game"); Frame.Navigate(typeof(BlankPage1)); }
Refer this article to learn for How to get number of Application Insights events from Visual Studio 2015?
Once you review the application insights counts with above code, append the following line of code.
TelemetryConfiguration.Active.DisableTelemetry = true;
At this point of time, if you put a breakpoint and review the Active Telemetry Configuration, you will find the same Instrumentation Key as per the Application.config file and the DisableTelemetry is set to True.
Now, if you try to verify the Application Insight events count or even debug window; you won’t find any entry.
How to make it disable only in Debug Mode ?
You can use the #pragrma to disable it only in case of Debug / any specific mode. So that without changing any code it can work on other configuration (such as Release )
#if DEBUG TelemetryConfiguration.Active.DisableTelemetry = true; #endif
You can also check to the question I referred earlier. It has few updated reference that talks about how to use “DeveloperMode” or even how to use multiple environment for Application Insights.
Hope this help
Pingback: .NET Tips and Tricks from Daily .NET Tips – ( Visual Studio 2015, Windows Universal Apps, Application Insights, Cross Platform Apps ) – August 2015 Links | Abhijit's World of .NET
Pingback: Dew Drop – September 2, 2015 (#2082) | Morning Dew
Pingback: Gulp for Beginners - The Daily Six Pack: September 3, 2015