|
3 | 3 |
|
4 | 4 | #if NETSTANDARD2_0
|
5 | 5 | using System;
|
| 6 | +using System.Collections.Generic; |
6 | 7 | using System.IO;
|
7 | 8 | using Microsoft.Extensions.Configuration;
|
8 | 9 | using NewRelic.Core;
|
@@ -39,30 +40,42 @@ private static IConfigurationRoot InitializeConfiguration()
|
39 | 40 | applicationDirectory = Directory.GetCurrentDirectory();
|
40 | 41 | }
|
41 | 42 |
|
| 43 | + // add default appsettings.json files to config builder |
42 | 44 | var builder = new ConfigurationBuilder()
|
43 | 45 | .SetBasePath(applicationDirectory)
|
44 | 46 | .AddJsonFile("appsettings.json", optional: true, reloadOnChange: false);
|
45 | 47 |
|
46 |
| - // Determine if there might be an environment-specific appsettings file |
47 |
| - var env = new SystemInterfaces.Environment(); |
48 |
| - var environment = env.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); |
49 |
| - if (string.IsNullOrEmpty(environment)) |
50 |
| - { |
51 |
| - environment = env.GetEnvironmentVariable("EnvironmentName"); |
52 |
| - } |
| 48 | + _appSettingsFilePaths = Path.Combine(applicationDirectory, "appsettings.json"); |
53 | 49 |
|
54 |
| - if (!string.IsNullOrEmpty(environment)) |
55 |
| - { |
56 |
| - builder.AddJsonFile($"appsettings.{environment}.json", optional: true, reloadOnChange: false); |
57 |
| - } |
58 |
| - |
59 |
| - var appSettingsPath = Path.Combine(applicationDirectory, "appsettings.json"); |
| 50 | + // Determine if there is a .NET environment configured, or default to "Production" |
| 51 | + var environment = GetDotnetEnvironment(); |
| 52 | + builder.AddJsonFile($"appsettings.{environment}.json", optional: true, reloadOnChange: false); |
60 | 53 | var appSettingsEnvPath = Path.Combine(applicationDirectory, $"appsettings.{environment}.json");
|
61 |
| - _appSettingsFilePaths = !string.IsNullOrEmpty(environment) ? string.Join(", ", appSettingsPath, appSettingsEnvPath) : appSettingsPath; |
| 54 | + _appSettingsFilePaths = string.Join(", ", _appSettingsFilePaths, appSettingsEnvPath); |
62 | 55 |
|
63 | 56 | return builder.Build();
|
64 | 57 | }
|
65 | 58 |
|
| 59 | + private static string GetDotnetEnvironment() |
| 60 | + { |
| 61 | + var env = new SystemInterfaces.Environment(); |
| 62 | + // Determine the environment (e.g. Production, Development, Staging, etc.) by considering the following env vars in order |
| 63 | + // "DOTNET_ENVIRONMENT" takes precedence over "ASPNETCORE_ENVIRONMENT", even for ASP.NET Core applications |
| 64 | + // EnvironmentName is proprietary to our agent and the behavior as of version 10.20 is to not take precedence over the .NET builtins |
| 65 | + var envVarsToCheck = new List<string>() { "DOTNET_ENVIRONMENT", "ASPNETCORE_ENVIRONMENT", "EnvironmentName" }; |
| 66 | + foreach ( var envVar in envVarsToCheck ) |
| 67 | + { |
| 68 | + var environment = env.GetEnvironmentVariable(envVar); |
| 69 | + if (!string.IsNullOrEmpty(environment)) |
| 70 | + { |
| 71 | + Log.Debug($".NET environment set to '{environment}' from env var '{envVar}'"); |
| 72 | + return environment; |
| 73 | + } |
| 74 | + } |
| 75 | + Log.Finest("No .NET environment configured in DOTNET_ENVIRONMENT, ASPNETCORE_ENVIRONMENT, or EnvironmentName. Defaulting to 'Production'"); |
| 76 | + return "Production"; |
| 77 | + } |
| 78 | + |
66 | 79 | public static string GetAppSetting(string key)
|
67 | 80 | {
|
68 | 81 | if (key == null)
|
|
0 commit comments