5

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()
 {
        #if DEBUG
        TelemetryConfiguration.Active.DisableTelemetry = true;
        #endif
 }

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

James Davis - MSFT
  • 1,736
  • 10
  • 12
jgill09
  • 151
  • 1
  • 2
  • 13

3 Answers3

5

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.

00000000-0000-0000-0000-000000000000

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";
        }
    }
}
James Davis - MSFT
  • 1,736
  • 10
  • 12
1

What is the concern you have of sending the telemetry in Application Insights? For example, you can have a separate Application Insights resource (identified by instrumentation key) that is only used for your debugging experience, and when in production, switch the instrumentation key to point to a production resource.

Alternatively, we have recently introduced a "local mode" - ability to use Application Insights in Visual Studio without connection to Azure. In this case, the telemetry from the last debug session is preserved on your local machine and available for Search and integration into Diagnostics Hub. See here: https://azure.microsoft.com/en-us/documentation/articles/app-insights-release-notes-vsix/#version-43

I believe that in order to achieve this, you need to remove the instrumentation key from the applicationinsights.xml, but I am not 100% certain. Will ask my colleagues to add more info here... Let us know if this is what you are seeking for. Oleg

Oleg Ananiev
  • 901
  • 8
  • 7
  • When debugging an application in Visual Studio which has Application Insights (AI) configured, it is possible to see the output of the AI messages directly in VS in "local mode" (rather than actually transmitted to a remote AI endpoint). What I have not come across is how to transmit logging and telemetry information to this local AI from external applications. For example, if my system has two distinct applications running -- or even a pure HTML single-page web application -- is it possible to connect additional applications to the local VS AI instance in debug mode? – Charles Chen Jul 21 '20 at 13:44
0

@DebugThings answer will (mostly) work, although if you use 0's for the ikey, you're probably still sending telemetry, but it is probably also being rejected by the AI backend as an invalid ikey.

Personally, the best solution would be to create a separate "debug" iKey, and in code when built in debug mode, use that iKey instead.

protected void Application_Start()
{
    #if DEBUG
    TelemetryConfiguration.Active.InstrumentationKey = "your debug ikey";
    #endif
}

This way, you can "debug" any telemetry that you are sending without polluting your production environment, and in release builds, the iKey in your config file will still be used. this lets you make sure you're sending the right custom properties / metrics without using up the fixed number you're allowed at this time.

Here's a blog post about using configurations to send data to different places depending on environment, etc:

https://blogs.msdn.microsoft.com/visualstudioalm/2015/01/07/application-insights-support-for-multiple-environments-stamps-and-app-versions/

there's a similar question here with similar answers: Disable application insights in debug

Community
  • 1
  • 1
John Gardner
  • 24,225
  • 5
  • 58
  • 76