I want Azure ApplicationInsights to only be used in an ASP.NET Core application if specified by a Configuration value that is set in appsettings.json.
I put this in my _Layout.cshml to disable client-side insights:
@if (Configuration.GetSection("UseInsights").Get<bool>())
{
@Html.Raw(JavaScriptSnippet.FullScript)
}
But server-side telemetry is still sent, so I added this when its disabled via Configuration:
TelemetryConfiguration.Active.DisableTelemetry = true;
This stopped Request and Exception data from being sent, but things like CPU usage were still being sent and visible on my Insights dashboard.
What I think would be the proper method is to enable the .UseApplicationInsights() extension on WebHost.CreateDefaultBuilder() conditionally, but this is a chicken and egg scenario since I don't have access to the Configuration settings until after the Host has been built. Is there somewhere I can fully disable insights after calling BuildWebHost()?