Skip to content

Commit 3c21fe9

Browse files
authored
fix: improve handling of .NET environments and settings from appsettings.*.json files (#2125)
* POC fix * Take DOTNET_ENVIRONMENT into account Also refactor * Refactor based on team pairing * Fix integration test
1 parent 7221cd8 commit 3c21fe9

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

src/Agent/NewRelic/Agent/Core/Configuration/AppSettingsConfigResolveWhenUsed.cs

+27-14
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#if NETSTANDARD2_0
55
using System;
6+
using System.Collections.Generic;
67
using System.IO;
78
using Microsoft.Extensions.Configuration;
89
using NewRelic.Core;
@@ -39,30 +40,42 @@ private static IConfigurationRoot InitializeConfiguration()
3940
applicationDirectory = Directory.GetCurrentDirectory();
4041
}
4142

43+
// add default appsettings.json files to config builder
4244
var builder = new ConfigurationBuilder()
4345
.SetBasePath(applicationDirectory)
4446
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false);
4547

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");
5349

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);
6053
var appSettingsEnvPath = Path.Combine(applicationDirectory, $"appsettings.{environment}.json");
61-
_appSettingsFilePaths = !string.IsNullOrEmpty(environment) ? string.Join(", ", appSettingsPath, appSettingsEnvPath) : appSettingsPath;
54+
_appSettingsFilePaths = string.Join(", ", _appSettingsFilePaths, appSettingsEnvPath);
6255

6356
return builder.Build();
6457
}
6558

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+
6679
public static string GetAppSetting(string key)
6780
{
6881
if (key == null)

tests/Agent/IntegrationTests/IntegrationTests/AgentFeatures/EnvironmentTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void TestConfigPaths()
6060
_connectData = _connectData ?? _fixture.AgentLog.GetConnectData();
6161

6262
var nrConfig = _connectData?.Environment?.GetPropertyString("Initial NewRelic Config");
63-
var appConfig = _connectData?.Environment?.GetPropertyString("Application Config");
63+
var appConfig = _connectData?.Environment?.GetPropertyString("Application Config").Split(',')[0];
6464

6565
NrAssert.Multiple(
6666
() => Assert.NotNull(nrConfig),

0 commit comments

Comments
 (0)