Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Azure App Service App settings not being picked up #72

Closed
rayrfan opened this issue Nov 23, 2017 · 0 comments
Closed

Fix Azure App Service App settings not being picked up #72

rayrfan opened this issue Nov 23, 2017 · 0 comments
Assignees
Labels
Azure azure related bug Configuration options in source, e.g. appsettings.json
Milestone

Comments

@rayrfan
Copy link
Owner

rayrfan commented Nov 23, 2017

Issue

I have a Azure App Service App setting for Application Insights Instrumentation key, but it's not being picked up.

Cause

I mistakenly removed .AddEnvironmentVariables() which is important for Azure App settings, without it those settings will not be picked up, see this commit d249584#commitcomment-25810929

Fix

I updated the code in Program.cs to debug

        var configuration = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables() // <-- important
            .AddCommandLine(args)
            .Build();

        var instrumentKey = configuration.GetValue<string>("ApplicationInsights:InstrumentationKey");
        Log.Logger = new LoggerConfiguration()
           .ReadFrom.Configuration(configuration)
           .Enrich.FromLogContext()
           .WriteTo.ApplicationInsightsTraces(instrumentKey)
           .CreateLogger();

            Log.Warning("Instrumentation Key: " + instrumentKey);

Things Learned

The key in Azure App settings can be either ApplicationInsights:InstrumentationKey or ApplicationInsights__InstrumentationKey.

ASPNETCORE_ENVIRONMENT on Azure seems to be not existed, so I default in my code for it to be "Production".

The hosting environment default value is "production", see
aspnet/Hosting#863
https://github.com/aspnet/Hosting/blob/b6da89f54cff11474f17486cdc55c2f21f2bbd6b/src/Microsoft.AspNetCore.Hosting/Internal/HostingEnvironment.cs#L10

   public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.cs.json")
            .AddJsonFile($"appsettings.cs.{env.EnvironmentName}.json", optional: true);

Therefore here, the default value will be production.

@rayrfan rayrfan added Configuration options in source, e.g. appsettings.json In Progress someone is working on it Settings options in a running site's admin console labels Nov 23, 2017
@rayrfan rayrfan added this to the 1.0.0-rc milestone Nov 23, 2017
@rayrfan rayrfan self-assigned this Nov 23, 2017
@rayrfan rayrfan added bug and removed Settings options in a running site's admin console labels Nov 23, 2017
@rayrfan rayrfan removed the In Progress someone is working on it label Nov 23, 2017
@rayrfan rayrfan closed this as completed Nov 23, 2017
@rayrfan rayrfan added the Azure azure related label Jun 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Azure azure related bug Configuration options in source, e.g. appsettings.json
Projects
No open projects
Development

No branches or pull requests

1 participant