Why is telemetryClient.IsEnabled() true if no key is provided

David Thielen 2,751 Reputation points
2024-07-12T18:56:14.73+00:00

Hi all;

I have the following code:

builder.Logging.AddApplicationInsights(); 
builder.Services.AddApplicationInsightsTelemetry();                builder.Services.ConfigureTelemetryModule<DependencyTrackingTelemetryModule>((module, o) => { module.EnableSqlCommandTextInstrumentation = true; });

// ...
 
var telemetryClient = app.Services.GetService<TelemetryClient>();
logger.LogInformation($"TelemetryClient = {(telemetryClient == null ? "null" : telemetryClient.IsEnabled())}");

I do not have the ApplicationInsights key set anywhere. Not in appSettings.json, not in the environment. I am running using the VisualStudio server for my Blazor app.

The above prints out true. Why is it true if the key is not set? (I need to use this to determine if I want to make calls to post events to the telemetry.)

thanks - dave

Azure Monitor
Azure Monitor
An Azure service that is used to collect, analyze, and act on telemetry data from Azure and on-premises environments.
2,988 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,493 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 50,916 Reputation points
    2024-07-12T20:23:37.4033333+00:00

    IsEnabled has nothing to do with whether the telemetry would actually work or not. The only way to know that is to actually try. There could be any # of things wrong such that the actual call fails, such as a missing or invalid key.

    All it looks at is whether it is enabled such that the code should even attempt to make the call. By default it is enabled and therefore always true. However telemetry can be disabled in the configuration which prevents it altogether.

    If you're interested, all this property really does is return TelemetryConfiguration.DisableTelemetry on the configuration associated with the client. The configuration is passed to the client instance when it is created.

    Now you may be wondering why the startup code is allowing you to register the services (which is ultimately what you're getting) without having the configuration right. That is likely because you can configure the telemetry multiple ways and ultimately the client will attempt to use it, which triggers the actual initialization. But simply getting the client and checking the IsEnabled property doesn't trigger any of this initialization, it is a simple property lookup.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful