Repository navigation
Description
Background and motivation
On Unix systems, the names of environment variables are are often limited to just letters, numbers and the underscore. (This is not a limitation of the OS, but of many tools, including bash or Azure App Service.) This makes it impossible to use the EnvironmentVariablesConfigurationProvider to configure components that expect such names in their configuration. In particular, Microsoft.Extensions.Logging expects hierarchical keys that can look like Logging:LogLevel:Microsoft.Hosting, where the last part is a namespace, so it will very often include dots.
EnvironmentVariablesConfigurationProvider already replaces __ (double underscore) with :, so that hierarchical keys could be represented at all. The proposal here is to effectively expand this capability and make it configurable.
API Proposal
namespace Microsoft.Extensions.Configuration.EnvironmentVariables;
public partial class EnvironmentVariablesConfigurationSource : IConfigurationSource
{
// existing
// public string? Prefix { get; set; }
public Func<string, string>? VariableNameTransformation { get; set; }
}
namespace Microsoft.Extensions.Configuration;
public partial static class EnvironmentVariablesExtensions
{
// existing
// public static IConfigurationBuilder AddEnvironmentVariables(
// this IConfigurationBuilder configurationBuilder);
// public static IConfigurationBuilder AddEnvironmentVariables(
// this IConfigurationBuilder configurationBuilder, string? prefix);
// public static IConfigurationBuilder AddEnvironmentVariables(
// this IConfigurationBuilder builder, Action<EnvironmentVariablesConfigurationSource>? configureSource);
public static IConfigurationBuilder AddEnvironmentVariables(
this IConfigurationBuilder configurationBuilder, string? prefix, Func<string, string>? variableNameTransformation);
}The built-in __ → : replacement happens after the user-specified transformation.
API Usage
The code to replace three underscores with a dot would be:
configurationBuilder.AddEnvironmentVariables(null, name => name.Replace("___", "."));With this, the environment variable assignment of Logging__LogLevel__Microsoft___Hosting=Debug would produce configuration equivalent to:
{
"Logging": {
"LogLevel": {
"Microsoft.Hosting": "Debug"
}
}
}Open Questions
- Should the user have some way to override the built-in
__→:replacement?
Alternative Designs
More focused
If we only consider the Microsoft.Extensions.Logging case, then we only need to handle the dot character:
public partial class EnvironmentVariablesConfigurationSource : IConfigurationSource
{
public string? DotReplacement { get; set; }
}In my opinion, this is too narrow, since any of the forbidden characters can be expected by a library that is being configured. Specifically, we had a report of a library that expected a configuration key with a hyphen (-) (it was since changed to also accept the same key without a hyphen, though).
Another option is to have a dictionary of replacements:
public partial class EnvironmentVariablesConfigurationSource : IConfigurationSource
{
public IDictionary<string, char>? VariableNameCharacterReplacements { get; set; }
}This lacks the flexibility of the proposed Func approach (e.g. with a Func, you could only perform the replacement for Logging keys, or any other number of customizations).
More general
Also allow arbitrary changes to values:
public partial class EnvironmentVariablesConfigurationSource : IConfigurationSource
{
public Func<(string Name, string Value), (string Name, string Value)>? VariableTransformation { get; set; }
}In my opinion, the generality of this alternative is not justified, since it lacks a compelling use case.
Risks
There shouldn't be any.
Activity
dotnet-policy-service commentedon Mar 3, 2026
Tagging subscribers to this area: @dotnet/area-extensions-configuration
See info in area-owners.md if you want to be subscribed.
MihuBot commentedon Mar 3, 2026
I'm a bot. Here are possible related and/or duplicate issues (I may be wrong):
mrek-msft commentedon Mar 4, 2026
Some validations will be needed. For example, following should be invalid, I think
Similarly it is questionable how should something like this behave or how to efficiently reject it
svick commentedon Mar 4, 2026
@mrek-msft Forbidding
__makes sense to me.The other variant should switch
!and?around, so I don't see a need to reject it, even though it indeed doesn't make sense.mrek-msft commentedon Mar 4, 2026
The
!and?example focus importance of order. Assuming most simple replacement strategy, something likeforeach (dict) { str = str.Replace(dict.Key, dict.Value) }. It will behave differently if you first replace!to?and then all?to!(in that case both symbols in original string becomes!. If you do that other way around, all symbols becomes?. In both case it is unlikely what user want, I think.And I think there is more issues with ordering. For example, it is important to thing about order of replacement of
__to:and new proposal. Depending on itwill convert
___(3x underscore) to.or not. This is what workaround #87130 (comment) utilizes.svick commentedon Mar 4, 2026
Yes. But instead of somehow validating that issues like these can't happen, I think it's better if we use a smarter replacement strategy that doesn't have ordering issues. (The logic would be: at each character position, find the longest replacement string that starts there, if there is one, and replace it with its character.)
Yes. I think
___needs to work and:_shouldn't work.jez9999 commentedon Mar 4, 2026
I still prefer the simpler idea I had and posted in #35989 - allow key/value pairs in variable values, in which you can put basically any character, which side-steps the whole issue:
The default EnvironmentVariablesConfigurationProvider should scan for any env vars names DOTNET[i] and interpret them as key/value pairs.
bartonjs commentedon Apr 14, 2026
Video
Add configurable VariableNameTransformation to EnvironmentVariables c…
svick commentedon Apr 28, 2026
During implementation, I have found out that an API was missing from the original proposal:
Under normal circumstances,
EnvironmentVariablesConfigurationProvideris created behind the scenes inEnvironmentVariablesConfigurationSource.Build(), but there are also more advanced use cases where it makes sense to create it directly, like composition of configuration providers or manual usage without DI.Add configurable VariableNameTransformation to EnvironmentVariables c…
jez9999 commentedon May 21, 2026
Hmm. #127503 isn't the way I would've done it (I still prefer my
DOTNETVAR1='foo=bar'idea) but it should work adequately, I guess.