diff --git a/src/Agent/NewRelic/Agent/Core/Configuration/AppSettingsConfigResolveWhenUsed.cs b/src/Agent/NewRelic/Agent/Core/Configuration/AppSettingsConfigResolveWhenUsed.cs index 20bbadae75..ffa78cb7e3 100644 --- a/src/Agent/NewRelic/Agent/Core/Configuration/AppSettingsConfigResolveWhenUsed.cs +++ b/src/Agent/NewRelic/Agent/Core/Configuration/AppSettingsConfigResolveWhenUsed.cs @@ -3,6 +3,7 @@ #if NETSTANDARD2_0 using System; +using System.Collections.Generic; using System.IO; using Microsoft.Extensions.Configuration; using NewRelic.Core; @@ -39,30 +40,42 @@ private static IConfigurationRoot InitializeConfiguration() applicationDirectory = Directory.GetCurrentDirectory(); } + // add default appsettings.json files to config builder var builder = new ConfigurationBuilder() .SetBasePath(applicationDirectory) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: false); - // Determine if there might be an environment-specific appsettings file - var env = new SystemInterfaces.Environment(); - var environment = env.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); - if (string.IsNullOrEmpty(environment)) - { - environment = env.GetEnvironmentVariable("EnvironmentName"); - } + _appSettingsFilePaths = Path.Combine(applicationDirectory, "appsettings.json"); - if (!string.IsNullOrEmpty(environment)) - { - builder.AddJsonFile($"appsettings.{environment}.json", optional: true, reloadOnChange: false); - } - - var appSettingsPath = Path.Combine(applicationDirectory, "appsettings.json"); + // Determine if there is a .NET environment configured, or default to "Production" + var environment = GetDotnetEnvironment(); + builder.AddJsonFile($"appsettings.{environment}.json", optional: true, reloadOnChange: false); var appSettingsEnvPath = Path.Combine(applicationDirectory, $"appsettings.{environment}.json"); - _appSettingsFilePaths = !string.IsNullOrEmpty(environment) ? string.Join(", ", appSettingsPath, appSettingsEnvPath) : appSettingsPath; + _appSettingsFilePaths = string.Join(", ", _appSettingsFilePaths, appSettingsEnvPath); return builder.Build(); } + private static string GetDotnetEnvironment() + { + var env = new SystemInterfaces.Environment(); + // Determine the environment (e.g. Production, Development, Staging, etc.) by considering the following env vars in order + // "DOTNET_ENVIRONMENT" takes precedence over "ASPNETCORE_ENVIRONMENT", even for ASP.NET Core applications + // EnvironmentName is proprietary to our agent and the behavior as of version 10.20 is to not take precedence over the .NET builtins + var envVarsToCheck = new List() { "DOTNET_ENVIRONMENT", "ASPNETCORE_ENVIRONMENT", "EnvironmentName" }; + foreach ( var envVar in envVarsToCheck ) + { + var environment = env.GetEnvironmentVariable(envVar); + if (!string.IsNullOrEmpty(environment)) + { + Log.Debug($".NET environment set to '{environment}' from env var '{envVar}'"); + return environment; + } + } + Log.Finest("No .NET environment configured in DOTNET_ENVIRONMENT, ASPNETCORE_ENVIRONMENT, or EnvironmentName. Defaulting to 'Production'"); + return "Production"; + } + public static string GetAppSetting(string key) { if (key == null) diff --git a/tests/Agent/IntegrationTests/IntegrationTests/AgentFeatures/EnvironmentTests.cs b/tests/Agent/IntegrationTests/IntegrationTests/AgentFeatures/EnvironmentTests.cs index 696dae4969..bbe89aa5a3 100644 --- a/tests/Agent/IntegrationTests/IntegrationTests/AgentFeatures/EnvironmentTests.cs +++ b/tests/Agent/IntegrationTests/IntegrationTests/AgentFeatures/EnvironmentTests.cs @@ -60,7 +60,7 @@ public void TestConfigPaths() _connectData = _connectData ?? _fixture.AgentLog.GetConnectData(); var nrConfig = _connectData?.Environment?.GetPropertyString("Initial NewRelic Config"); - var appConfig = _connectData?.Environment?.GetPropertyString("Application Config"); + var appConfig = _connectData?.Environment?.GetPropertyString("Application Config").Split(',')[0]; NrAssert.Multiple( () => Assert.NotNull(nrConfig),