jgill09 jgill09 - 2 months ago 5x
C# Question

Application Insights when in debug mode

I've just enabled Application Insights in my MVC application and noticed that when debugging locally, that the trace information is being captured within my Azure Application Insight.

When in debug mode, I want to stop my app from recording the events within my Azure Application Insight but still display the Events and logging information in the Diagnostic Tools > Events window within Visual Studio.

I've tried the following and whilst this stop the events from being captured in my Azure AI, Visual Studio no longer displays the debug information in Events window.

protected void Application_Start()
TelemetryConfiguration.Active.DisableTelemetry = true;

I've surfed the net for an answer to no avail. Hope someone can help.


The cheapest way to do this is to set your Instrumentation Key to all 0's. There is no NULL iKey so it effectively will just drop the message.


If you want to use Application_Start() you can do this using either the #DEBUG directive or you can use System.Diagnostics.Debugger.IsAttached property. However this method is not completely reliable. You can try but your experience may not be consistent.

If you have the time you should make a TelemetryInitializer that would change the Instrumentation Key based on if the debugger is attached or not. This will make sure this only happens if you're inside a debugging session. That way if you accidentally release Debug to production you won't lose your telemetry.

public class CustomeWebRequestTelemetryModule :  Microsoft.ApplicationInsights.Extensibility.ITelemetryInitializer
    public void Initialize(ITelemetry telemetry)
        if (telemetry != null && System.Diagnostics.Debugger.IsAttached)
            telemetry.Context.InstrumentationKey = "00000000-0000-0000-0000-000000000000";