Skip to content
This repository was archived by the owner on Jun 10, 2020. It is now read-only.
This repository was archived by the owner on Jun 10, 2020. It is now read-only.

IWebHostBuilder UseApplicationInsights Extension Doesn't Use Environment Specific Key #449

@billpratt

Description

@billpratt

When wiring up application insights using the WebHostBuilder extension, it seems to ignore the environment specific instrumentation key from appsettings.ENVIRONMENT.json

var host = new WebHostBuilder()
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseStartup<Startup>()
                .UseApplicationInsights()
                .Build();

            host.Run();

Method used: https://github.com/Microsoft/ApplicationInsights-aspnetcore/blob/develop/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsWebHostBuilderExtensions.cs#L18

Looking at the source I see that its using DefaultApplicationInsightsServiceConfigureOptions which uses its own ConfigurationBuilder and only adds "appsettings.json" and not appsettings.ENVIRONMENT.json.

public void Configure(ApplicationInsightsServiceOptions options)
{
    var configBuilder = new ConfigurationBuilder()
        .SetBasePath(this.hostingEnvironment.ContentRootPath)
        .AddJsonFile("appsettings.json", true)
        .AddEnvironmentVariables();
    ApplicationInsightsExtensions.AddTelemetryConfiguration(configBuilder.Build(), options);

    if (Debugger.IsAttached)
    {
        options.DeveloperMode = true;
    }
}

See: https://github.com/Microsoft/ApplicationInsights-aspnetcore/blob/develop/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/DefaultApplicationInsightsServiceConfigureOptions.cs#L31

  • Was this by design?
  • Is using different app insight resources for each environment not a valid approach moving forward?
  • Since the File->New->Project wires up app insights using the WebHostBuilder this seems like a gotcha for users that need to override the instrumentation key per environment.

I will be happy to submit a PR for review if this was an oversight!

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions