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: improve handling of .NET environments and settings from appsettings.*.json files #2125

Merged
merged 4 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading