How to disable the Application Insights while in Debug ?

How to disable the Application Insights while in Debug ?

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.

Microsoft.ApplicationInsights.Extensibility namespaces
Adding Microsoft.ApplicationInsights.Extensibility namespaces

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;

 

image

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.

elemetry events

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

Abhijit Jana

Abhijit runs the Daily .NET Tips. He started this site with a vision to have a single knowledge base of .NET tips and tricks and share post that can quickly help any developers . He is a Former Microsoft ASP.NET MVP, CodeProject MVP, Mentor, Speaker, Author, Technology Evangelist and presently working as a .NET Consultant. He blogs at http://abhijitjana.net , you can follow him @AbhijitJana . He is the author of book Kinect for Windows SDK Programming Guide.