Search code examples
azureazure-web-app-serviceazure-application-insights

How can I disable Application Insights in my code?


I have a Blazor Interactive Server app. It is connected to Application Insights via the Application Insights SDK.

How can I disable Application Insights for some cases?

Here’s our situation. We have several developers running the app on their desktop while running/testing code. They never want Application Insights running. They then push their code up to GitHub where it is tested and then pushed to the dev slot. We want Application Insights running on the dev slot only when we’re load testing. Then once all the tests pass, we swap the dev & production slots. We always want the production slot using Application Insights.

Application Insights is already turned off in the Azure App Server blade because we added it using the SDK. We can’t remove the SDK because the final destination of the code, the production slot, needs it incorporated and running.

If there’s no other way to set it as off, then passing an invalid connection string is probably the best approach. But I dislike doing this as it’s purposely causing an error condition. And calls to the Application Insights methods, such as tracking the time for a search, then on each call, execute code passing that down, and then hitting an error when it tries to send it.

Is there something that allows Application Insights to be initialized but tells it to quickly ignore any calls to it as it’s disabled?


Solution

  • does it stop it from connecting to Application Insights itself so it receives nothing about this server?

    To completely stop the local Environment from connecting with the Application Insights, you can pass a connection string only for the Production Environment.

    var environment = builder.Environment;
    if (environment.IsProduction())
    {
        builder.Services.AddApplicationInsightsTelemetry(new Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions
        {
            ConnectionString = builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]
        });
    }
    
    • Log Information traces will be traced only for the deployed app service.
    var app = builder.Build();
    
    var log = app.Services.GetRequiredService<ILogger<Program>>();
    log.LogInformation($"Environment : {environment.EnvironmentName}"); // Log the environment name
    log.LogInformation("Information Logs");
    log.LogInformation("Production Logs");
    log.LogInformation("No local");
    
    • To verify the Environment, I am tracing the Hosting Environment.

    enter image description here

    • You can observe no traces will be seen in local console related to Application Insights.

    enter image description here

    • In older version of Visual Studi0 2019, you can see an option to disable Application Insights in the Tools => Options => Web Projects under Projects and Solutions.

    enter image description here

    does it stop it from connecting to Application Insights itself so it receives nothing about this server?

    You can see the similar issue discussed in GitHub

    Thanks @Callon Campbell for the explanation.

    • I have tried to use the TelemetryConfiguration.Active.DisableTelemetry property,
    if(.IsDevelopment())
    {
      TelemetryConfiguration.Active.DisableTelemetry=true;
    }
    

    but you can see the property is obsolete now and not a recommended way.

    enter image description here

    • The easy way is to pass the connection string only for the production environment (which I have mentioned in the initial steps).