Skip to content

Commit

Permalink
fix: improve handling of .NET environments and settings from appsetti…
Browse files Browse the repository at this point in the history
…ngs.*.json files (#2125)

* POC fix

* Take DOTNET_ENVIRONMENT into account

Also refactor

* Refactor based on team pairing

* Fix integration test
  • Loading branch information
nr-ahemsath committed Dec 7, 2023
1 parent 7221cd8 commit 3c21fe9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#if NETSTANDARD2_0
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Extensions.Configuration;
using NewRelic.Core;
Expand Down Expand Up @@ -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<string>() { "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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 3c21fe9

Please sign in to comment.