Skip to content

Add option to fail fast on configuration errors in AddApplicationInsightsTelemetry #2988

@JJong-nl

Description

@JJong-nl

Is your feature request related to a problem?
In production environments, observability is often considered critical — the system should fail to start if telemetry is not properly configured. However, currently AddApplicationInsightsTelemetry* and OpenTelemetry registration fail silently on configuration errors (e.g., missing or invalid connection string), which can lead to applications running without monitoring or visibility.

As an example, this makes it hard to detect when TelemetryClient is not available in DI, causing runtime exceptions in code that depends on it (e.g., decorators, middlewares).

Describe the solution you'd like.
A boolean option like FailOnConfigurationError or ThrowOnRegistrationFailure, that:

  • Logs the original error (as today),
  • But then throws an exception, stopping the application startup,
  • Is opt-in, so the current behavior remains the default for backward compatibility.

Describe alternatives you've considered.

  • Manually calling GetRequiredService() in Program.cs to detect failure at startup.
  • Implementing a StartupValidator pattern.
  • Wrapping TelemetryClient in a NullObject pattern.

While these workarounds are possible, they are not ideal - the telemetry libraries already detect the problem, but do not expose a way to escalate it.

Additional context.
Other frameworks and libraries (like ASP.NET Core DI, Serilog, Entity Framework) offer similar fail-fast options for critical services. This would bring parity to observability setup and make it easier to ensure production readiness.

Suggested API

builder.Services.AddApplicationInsightsTelemetryWorkerService(options =>
{
    options.ConnectionString = "...";
    options.FailOnConfigurationError = true;
});

public static IServiceCollection AddApplicationInsightsTelemetryWorkerService(this IServiceCollection services)
{
try
{
if (!IsApplicationInsightsAdded(services))
{
services.AddSingleton<ITelemetryInitializer, AzureWebAppRoleEnvironmentTelemetryInitializer>();
AddCommonInitializers(services);
AddCommonTelemetryModules(services);
AddTelemetryChannel(services);
services
.TryAddSingleton<IConfigureOptions<ApplicationInsightsServiceOptions>,
DefaultApplicationInsightsServiceConfigureOptions>();
AddDefaultApplicationIdProvider(services);
AddTelemetryConfigAndClient(services);
AddApplicationInsightsLoggerProvider(services);
}
return services;
}
catch (Exception e)
{
WorkerServiceEventSource.Instance.LogError(e.ToInvariantString());
return services;
}
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions