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

Remove handwritten binding logic from Logging.Console; use generator instead #88067

Merged
merged 7 commits into from
Jul 14, 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 @@ -30,7 +30,7 @@ private void LoadDefaultConfigValues(LoggerFilterOptions options)
return;
}

options.CaptureScopes = GetCaptureScopesValue(options);
options.CaptureScopes = _configuration.GetValue(nameof(options.CaptureScopes), options.CaptureScopes);

foreach (IConfigurationSection configurationSection in _configuration.GetChildren())
{
Expand All @@ -50,8 +50,6 @@ private void LoadDefaultConfigValues(LoggerFilterOptions options)
}
}
}

bool GetCaptureScopesValue(LoggerFilterOptions options) => _configuration.GetValue(nameof(options.CaptureScopes), options.CaptureScopes);
}

private static void LoadRules(LoggerFilterOptions options, IConfigurationSection configurationSection, string? logger)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Configuration.Binder\gen\Microsoft.Extensions.Configuration.Binder.SourceGeneration.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="true" PrivateAssets="All" />
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Configuration.Binder\gen\Microsoft.Extensions.Configuration.Binder.SourceGeneration.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" PrivateAssets="All" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,6 @@ public ConsoleFormatterOptions() { }
/// </summary>
public bool UseUtcTimestamp { get; set; }

internal virtual void Configure(IConfiguration configuration)
{
if (ConsoleLoggerOptions.ParseBool(configuration, nameof(IncludeScopes), out bool includeScopes))
{
IncludeScopes = includeScopes;
}
layomia marked this conversation as resolved.
Show resolved Hide resolved

if (configuration[nameof(TimestampFormat)] is string timestampFormat)
{
TimestampFormat = timestampFormat;
}

if (ConsoleLoggerOptions.ParseBool(configuration, nameof(UseUtcTimestamp), out bool useUtcTimestamp))
{
UseUtcTimestamp = useUtcTimestamp;
}
}
internal virtual void Configure(IConfiguration configuration) => configuration.Bind(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public ConsoleLoggerConfigureOptions(ILoggerProviderConfiguration<ConsoleLoggerP
_configuration = providerConfiguration.Configuration;
}

public void Configure(ConsoleLoggerOptions options) => options.Configure(_configuration);
public void Configure(ConsoleLoggerOptions options) => _configuration.Bind(options);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ private ConsoleLoggerSettingsAdapter(IConsoleLoggerSettings settings)

IChangeToken IOptionsChangeTokenSource<ConsoleLoggerOptions>.GetChangeToken() => _settings.ChangeToken ?? NullChangeToken.Instance;

string IOptionsChangeTokenSource<ConsoleLoggerOptions>.Name => Microsoft.Extensions.Options.Options.DefaultName;
string IOptionsChangeTokenSource<ConsoleLoggerOptions>.Name => Options.Options.DefaultName;

void IConfigureOptions<ConsoleLoggerOptions>.Configure(ConsoleLoggerOptions options)
{
options.IncludeScopes = _settings.IncludeScopes;
if (_settings is ConfigurationConsoleLoggerSettings configSettings)
{
options.Configure(configSettings._configuration);
configSettings._configuration.Bind(options);
layomia marked this conversation as resolved.
Show resolved Hide resolved
}
else if (_settings is ConsoleLoggerSettings consoleSettings)
{
Expand All @@ -105,7 +105,7 @@ void IConfigureOptions<ConsoleLoggerOptions>.Configure(ConsoleLoggerOptions opti
internal static OptionsMonitor<ConsoleLoggerOptions> GetOptionsMonitor(IConsoleLoggerSettings settings)
{
ConsoleLoggerSettingsAdapter adapter = new(settings);
OptionsFactory<ConsoleLoggerOptions> factory = new( new IConfigureOptions<ConsoleLoggerOptions>[] { adapter }, Array.Empty<IPostConfigureOptions<ConsoleLoggerOptions>>());
OptionsFactory<ConsoleLoggerOptions> factory = new(new IConfigureOptions<ConsoleLoggerOptions>[] { adapter }, Array.Empty<IPostConfigureOptions<ConsoleLoggerOptions>>());
IOptionsChangeTokenSource<ConsoleLoggerOptions>[] sources = new IOptionsChangeTokenSource<ConsoleLoggerOptions>[] { adapter };
OptionsCache<ConsoleLoggerOptions> cache = new();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Globalization;
using Microsoft.Extensions.Configuration;

namespace Microsoft.Extensions.Logging.Console
{
Expand Down Expand Up @@ -102,146 +100,5 @@ public int MaxQueueLength
_maxQueuedMessages = value;
}
}

internal void Configure(IConfiguration configuration)
{
#pragma warning disable CS0618 // Type or member is obsolete
if (ParseBool(configuration, nameof(DisableColors), out bool disableColors))
{
DisableColors = disableColors;
}
#pragma warning restore CS0618 // Type or member is obsolete

#pragma warning disable CS0618 // Type or member is obsolete
if (ParseEnum(configuration, nameof(Format), out ConsoleLoggerFormat format))
{
Format = format;
}
#pragma warning restore CS0618 // Type or member is obsolete

if (configuration[nameof(FormatterName)] is string formatterName)
{
FormatterName = formatterName;
}

#pragma warning disable CS0618 // Type or member is obsolete
if (ParseBool(configuration, nameof(IncludeScopes), out bool includeScopes))
{
IncludeScopes = includeScopes;
}
#pragma warning restore CS0618 // Type or member is obsolete

if (ParseEnum(configuration, nameof(LogToStandardErrorThreshold), out LogLevel logToStandardErrorThreshold))
{
LogToStandardErrorThreshold = logToStandardErrorThreshold;
}

if (ParseInt(configuration, nameof(MaxQueueLength), out int maxQueueLength))
{
MaxQueueLength = maxQueueLength;
}

if (ParseEnum(configuration, nameof(QueueFullMode), out ConsoleLoggerQueueFullMode queueFullMode))
{
QueueFullMode = queueFullMode;
}

#pragma warning disable CS0618 // Type or member is obsolete
if (configuration[nameof(TimestampFormat)] is string timestampFormat)
{
TimestampFormat = timestampFormat;
}
#pragma warning restore CS0618 // Type or member is obsolete

#pragma warning disable CS0618 // Type or member is obsolete
if (ParseBool(configuration, nameof(UseUtcTimestamp), out bool useUtcTimestamp))
{
UseUtcTimestamp = useUtcTimestamp;
}
#pragma warning restore CS0618 // Type or member is obsolete
}

/// <summary>
/// Parses the configuration value at the specified key into a bool.
/// </summary>
/// <returns>true if the value was successfully found and parsed. false if the key wasn't found.</returns>
/// <exception cref="InvalidOperationException">Thrown when invalid data was found at the specified configuration key.</exception>
internal static bool ParseBool(IConfiguration configuration, string key, out bool value)
{
if (configuration[key] is string valueString)
{
try
{
value = bool.Parse(valueString);
return true;
}
catch (Exception e)
{
ThrowInvalidConfigurationException(configuration, key, typeof(bool), e);
}
}

value = default;
return false;
}

/// <summary>
/// Parses the configuration value at the specified key into an enum.
/// </summary>
/// <returns>true if the value was successfully found and parsed. false if the key wasn't found.</returns>
/// <exception cref="InvalidOperationException">Thrown when invalid data was found at the specified configuration key.</exception>
internal static bool ParseEnum<T>(IConfiguration configuration, string key, out T value) where T : struct
{
if (configuration[key] is string valueString)
{
try
{
value =
#if NETFRAMEWORK || NETSTANDARD2_0
(T)Enum.Parse(typeof(T), valueString, ignoreCase: true);
#else
Enum.Parse<T>(valueString, ignoreCase: true);
#endif
layomia marked this conversation as resolved.
Show resolved Hide resolved
return true;
}
catch (Exception e)
{
ThrowInvalidConfigurationException(configuration, key, typeof(T), e);
}
}

value = default;
return false;
}

/// <summary>
/// Parses the configuration value at the specified key into an int.
/// </summary>
/// <returns>true if the value was successfully found and parsed. false if the key wasn't found.</returns>
/// <exception cref="InvalidOperationException">Thrown when invalid data was found at the specified configuration key.</exception>
internal static bool ParseInt(IConfiguration configuration, string key, out int value)
{
if (configuration[key] is string valueString)
{
try
{
value = int.Parse(valueString, NumberStyles.Integer, NumberFormatInfo.InvariantInfo);
return true;
}
catch (Exception e)
{
ThrowInvalidConfigurationException(configuration, key, typeof(int), e);
}
}

value = default;
return false;
}

private static void ThrowInvalidConfigurationException(IConfiguration configuration, string key, Type valueType, Exception innerException)
{
IConfigurationSection section = configuration.GetSection(key);
throw new InvalidOperationException(SR.Format(SR.InvalidConfigurationData, section.Path, valueType), innerException);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,6 @@ public JsonConsoleFormatterOptions() { }
/// </summary>
public JsonWriterOptions JsonWriterOptions { get; set; }

internal override void Configure(IConfiguration configuration)
{
base.Configure(configuration);

if (configuration.GetSection(nameof(JsonWriterOptions)) is IConfigurationSection jsonWriterOptionsConfig)
{
JsonWriterOptions jsonWriterOptions = JsonWriterOptions;

if (ConsoleLoggerOptions.ParseBool(jsonWriterOptionsConfig, nameof(JsonWriterOptions.Indented), out bool indented))
{
jsonWriterOptions.Indented = indented;
}

if (ConsoleLoggerOptions.ParseInt(jsonWriterOptionsConfig, nameof(JsonWriterOptions.MaxDepth), out int maxDepth))
{
jsonWriterOptions.MaxDepth = maxDepth;
}

if (ConsoleLoggerOptions.ParseBool(jsonWriterOptionsConfig, nameof(JsonWriterOptions.SkipValidation), out bool skipValidation))
{
jsonWriterOptions.SkipValidation = skipValidation;
}

JsonWriterOptions = jsonWriterOptions;
}
}
internal override void Configure(IConfiguration configuration) => configuration.Bind(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,24 @@
<DefineConstants>$(DefineConstants);NO_SUPPRESS_GC_TRANSITION</DefineConstants>
<IncludePlatformAttributes>true</IncludePlatformAttributes>
<IsPackable>true</IsPackable>
<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
<!-- M.E.Configuration.Binder.SourceGeneration: JavaScriptEncoder not supported; no logic generated which is okay. Remove this when https://github.com/dotnet/runtime/issues/88865 is fixed. -->
<NoWarn>$(NoWarn);SYSLIB1100</NoWarn>
<PackageDescription>Console logger provider implementation for Microsoft.Extensions.Logging.</PackageDescription>
</PropertyGroup>

<ItemGroup>
<Compile Include="$(CommonPath)Extensions\Logging\NullExternalScopeProvider.cs"
Link="Common\src\Extensions\Logging\NullExternalScopeProvider.cs" />
<Compile Include="$(CommonPath)Extensions\Logging\NullScope.cs"
Link="Common\src\Extensions\Logging\NullScope.cs" />
<Compile Include="$(CommonPath)System\Text\Json\PooledByteBufferWriter.cs"
Link="Common\System\Text\Json\PooledByteBufferWriter.cs" />
<Compile Include="$(CommonPath)System\ThrowHelper.cs"
Link="Common\System\ThrowHelper.cs" />
<Compile Include="$(CommonPath)Extensions\Logging\NullExternalScopeProvider.cs" Link="Common\src\Extensions\Logging\NullExternalScopeProvider.cs" />
<Compile Include="$(CommonPath)Extensions\Logging\NullScope.cs" Link="Common\src\Extensions\Logging\NullScope.cs" />
<Compile Include="$(CommonPath)System\Text\Json\PooledByteBufferWriter.cs" Link="Common\System\Text\Json\PooledByteBufferWriter.cs" />
<Compile Include="$(CommonPath)System\ThrowHelper.cs" Link="Common\System\ThrowHelper.cs" />
</ItemGroup>

<ItemGroup>
<Compile Include="$(CommonPath)Interop\Windows\Interop.Libraries.cs"
Link="Common\Interop\Windows\Interop.Libraries.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.GetConsoleMode.cs"
Link="Common\Interop\Windows\Interop.GetConsoleMode.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.GetStdHandle.cs"
Link="Common\Interop\Windows\Interop.GetStdHandle.cs" />
<Compile Include="$(CommonPath)System\Console\ConsoleUtils.cs"
Link="Common\System\Console\ConsoleUtils.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Interop.Libraries.cs" Link="Common\Interop\Windows\Interop.Libraries.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.GetConsoleMode.cs" Link="Common\Interop\Windows\Interop.GetConsoleMode.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.GetStdHandle.cs" Link="Common\Interop\Windows\Interop.GetStdHandle.cs" />
<Compile Include="$(CommonPath)System\Console\ConsoleUtils.cs" Link="Common\System\Console\ConsoleUtils.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'">
Expand Down Expand Up @@ -67,4 +62,8 @@
<Reference Include="System.Runtime" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Configuration.Binder\gen\Microsoft.Extensions.Configuration.Binder.SourceGeneration.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" PrivateAssets="All" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,6 @@ public SimpleConsoleFormatterOptions() { }
/// </summary>
public bool SingleLine { get; set; }

internal override void Configure(IConfiguration configuration)
{
base.Configure(configuration);

if (ConsoleLoggerOptions.ParseEnum(configuration, nameof(ColorBehavior), out LoggerColorBehavior colorBehavior))
{
ColorBehavior = colorBehavior;
}

if (ConsoleLoggerOptions.ParseBool(configuration, nameof(SingleLine), out bool singleLine))
{
SingleLine = singleLine;
}
}
internal override void Configure(IConfiguration configuration) => configuration.Bind(this);
}
}