Search code examples
c#asp.net.netasp.net-mvcazure-application-insights

Need to prevent Application Insights from initializing when in debug in a web app


I have an MVC web application. It conditionally initializes test data in a database during Application_Start() while running in the debug config. This initialization is exceedingly slow apparently thanks to a fellow developer adding application insights tracking to the project. You see, every time we save to the DB via entity framework, we now hit the following on the callstack:

System.Web.dll!System.Web.HttpContext.Request.get()
Microsoft.AI.Web.dll!Microsoft.ApplicationInsights.Web.Implementation.HttpContextExtensions.GetRequest(System.Web.HttpContext context)
Microsoft.AI.Web.dll!Microsoft.ApplicationInsights.Web.Implementation.WebTelemetryInitializerBase.Initialize(Microsoft.ApplicationInsights.Channel.ITelemetry telemetry)
Microsoft.ApplicationInsights.dll!Microsoft.ApplicationInsights.TelemetryClient.Initialize(Microsoft.ApplicationInsights.Channel.ITelemetry telemetry)

HttpContext.Request.Get()!!! No kidding this'll be slow. We are doing lots of saves during the data population. Granted, there are optimizations to be had here, but this is a hairy ball of mess that I didn't write and would rather avoid cracking open.

Basically, I want Application Insights to stop doing any web requests. Preferably, it would do nothing at all in debug, but as long as it performs well, I don't mind. I've tried setting:

TelemetryConfiguration.Active.DisableTelemetry = true;

but this apparently doesn't affect whatever codepath is calling TelemetryClient.Initialize(). Any pointers?


Solution

  • The best way I know of to stop AI from running is to use a web.config transform for your Debug configuration to remove it. This will ensure it does not register for any web requests.

    Contents of Web.Debug.config

    <?xml version="1.0"?>
    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
      <system.web>
          <httpModules>
            <add xdt:Transform="Remove" xdt:Locator="Match(name)" name="ApplicationInsightsWebTracking" />
          </httpModules>
      </system.web>
      <system.webServer>
        <modules>
          <add xdt:Transform="Remove" xdt:Locator="Match(name)" name="ApplicationInsightsWebTracking"  />
        </modules>
      </system.webServer>
    </configuration>