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?
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"]
});
}
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");
Tools
=> Options
=> Web Projects
under Projects
and Solutions
.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.
TelemetryConfiguration.Active.DisableTelemetry
property,if(.IsDevelopment())
{
TelemetryConfiguration.Active.DisableTelemetry=true;
}
but you can see the property is obsolete now and not a recommended way.