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

Minimumlevel partial fix with IConfigurationRoot.Providers #233

Merged
merged 2 commits into from
Sep 13, 2022
Merged
Changes from 1 commit
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 @@ -22,12 +22,19 @@ class ConfigurationReader : IConfigurationReader
readonly IConfigurationSection _section;
readonly IReadOnlyCollection<Assembly> _configurationAssemblies;
readonly ResolutionContext _resolutionContext;
#if NETSTANDARD || NET461
readonly IConfigurationRoot _configurationRoot;
#endif

public ConfigurationReader(IConfigurationSection configSection, AssemblyFinder assemblyFinder, IConfiguration configuration = null)
{
_section = configSection ?? throw new ArgumentNullException(nameof(configSection));
_configurationAssemblies = LoadConfigurationAssemblies(_section, assemblyFinder);
_resolutionContext = new ResolutionContext(configuration);
#if NETSTANDARD || NET461
_configurationRoot = configuration as IConfigurationRoot;
#endif

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

}

// Used internally for processing nested configuration sections -- see GetMethodCalls below.
Expand Down Expand Up @@ -85,7 +92,7 @@ void ApplyMinimumLevel(LoggerConfiguration loggerConfiguration)
{
var minimumLevelDirective = _section.GetSection("MinimumLevel");

var defaultMinLevelDirective = minimumLevelDirective.Value != null ? minimumLevelDirective : minimumLevelDirective.GetSection("Default");
IConfigurationSection defaultMinLevelDirective = GetDefaultMinLevelDirective();
if (defaultMinLevelDirective.Value != null)
{
ApplyMinimumLevel(defaultMinLevelDirective, (configuration, levelSwitch) => configuration.ControlledBy(levelSwitch));
Expand Down Expand Up @@ -124,6 +131,35 @@ void ApplyMinimumLevel(IConfigurationSection directive, Action<LoggerMinimumLeve

SubscribeToLoggingLevelChanges(directive, levelSwitch);
}

IConfigurationSection GetDefaultMinLevelDirective()
{
#if NETSTANDARD || NET461

var defaultLevelDirective = minimumLevelDirective.GetSection("Default");
if (_configurationRoot != null && minimumLevelDirective.Value != null && defaultLevelDirective.Value != null)
{
foreach (var provider in _configurationRoot.Providers.Reverse())
{
if (provider.TryGet(minimumLevelDirective.Path, out _))
{
return _configurationRoot.GetSection(minimumLevelDirective.Path);
}

if (provider.TryGet(defaultLevelDirective.Path, out _))
{
return _configurationRoot.GetSection(defaultLevelDirective.Path);
}
}

return null;
}

#endif //NET451 or fallback

return minimumLevelDirective.Value != null ? minimumLevelDirective : minimumLevelDirective.GetSection("Default");

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

}
}

void SubscribeToLoggingLevelChanges(IConfigurationSection levelSection, LoggingLevelSwitch levelSwitch)
Expand Down