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

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

Closed
billpratt opened this issue May 24, 2017 · 2 comments
Assignees
Labels

Comments

@billpratt
Copy link
Contributor

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!

@dnduffy dnduffy added the bug label May 25, 2017
@dnduffy dnduffy self-assigned this May 25, 2017
@dnduffy
Copy link
Member

dnduffy commented May 25, 2017

@billpratt It was an oversight, please feel free to submit a PR. Thank-you.

@billpratt
Copy link
Contributor Author

@dnduffy Done here #450

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants