Uh oh!
There was an error while loading. Please reload this page.
Remove handwritten binding logic from Logging.Console; use generator instead - #88067
Conversation
ghost
commented
Jun 26, 2023
Tagging subscribers to this area: @dotnet/area-extensions-logging Issue DetailsGenerated code (click to view)// <auto-generated/>
#nullable enable
/// <summary>Generated helper providing an AOT and linking compatible implementation for configuration binding.</summary>internalstaticclassGeneratedConfigurationBinder{/// <summary>Attempts to bind the given object instance to configuration values by matching property names against configuration keys recursively.</summary>publicstaticvoidBind(thisglobal::Microsoft.Extensions.Configuration.IConfigurationconfiguration,global::Microsoft.Extensions.Logging.Console.ConsoleFormatterOptionsobj)=>global::Microsoft.Extensions.Configuration.Binder.SourceGeneration.CoreBindingHelper.BindCore(configuration,refobj,binderOptions:null);/// <summary>Attempts to bind the given object instance to configuration values by matching property names against configuration keys recursively.</summary>publicstaticvoidBind(thisglobal::Microsoft.Extensions.Configuration.IConfigurationconfiguration,global::Microsoft.Extensions.Logging.Console.ConsoleLoggerOptionsobj)=>global::Microsoft.Extensions.Configuration.Binder.SourceGeneration.CoreBindingHelper.BindCore(configuration,refobj,binderOptions:null);/// <summary>Attempts to bind the given object instance to configuration values by matching property names against configuration keys recursively.</summary>publicstaticvoidBind(thisglobal::Microsoft.Extensions.Configuration.IConfigurationconfiguration,global::Microsoft.Extensions.Logging.Console.JsonConsoleFormatterOptionsobj)=>global::Microsoft.Extensions.Configuration.Binder.SourceGeneration.CoreBindingHelper.BindCore(configuration,refobj,binderOptions:null);/// <summary>Attempts to bind the given object instance to configuration values by matching property names against configuration keys recursively.</summary>publicstaticvoidBind(thisglobal::Microsoft.Extensions.Configuration.IConfigurationconfiguration,global::Microsoft.Extensions.Logging.Console.SimpleConsoleFormatterOptionsobj)=>global::Microsoft.Extensions.Configuration.Binder.SourceGeneration.CoreBindingHelper.BindCore(configuration,refobj,binderOptions:null);}namespaceMicrosoft.Extensions.Configuration.Binder.SourceGeneration{usingMicrosoft.Extensions.Configuration;usingMicrosoft.Extensions.Logging;usingMicrosoft.Extensions.Logging.Console;usingSystem;usingSystem.Collections.Generic;usingSystem.Globalization;usingSystem.Text.Encodings.Web;usingSystem.Text.Json;/// <summary>Provide core binding logic.</summary>internalstaticclassCoreBindingHelper{publicstaticvoidBindCore(IConfigurationconfiguration,refConsoleFormatterOptionsobj,BinderOptions?binderOptions){if(objisnull){thrownewArgumentNullException(nameof(obj));}List<string>?temp=null;foreach(IConfigurationSectionsectioninconfiguration.GetChildren()){switch(section.Key){case"IncludeScopes":{if(configuration["IncludeScopes"]isstringstringValue0){obj.IncludeScopes=ParseBool(stringValue0,()=>section.Path)!;}}break;case"TimestampFormat":{obj.TimestampFormat=configuration["TimestampFormat"]!;}break;case"UseUtcTimestamp":{if(configuration["UseUtcTimestamp"]isstringstringValue2){obj.UseUtcTimestamp=ParseBool(stringValue2,()=>section.Path)!;}}break;default:{if(binderOptions?.ErrorOnUnknownConfiguration==true){(temp??=newList<string>()).Add($"'{section.Key}'");}}break;}}if(tempis not null){thrownewInvalidOperationException($"'ErrorOnUnknownConfiguration' was set on the provided BinderOptions, but the following properties were not found on the instance of {typeof(ConsoleFormatterOptions)}: {string.Join(", ",temp)}");}}publicstaticvoidBindCore(IConfigurationconfiguration,refConsoleLoggerOptionsobj,BinderOptions?binderOptions){if(objisnull){thrownewArgumentNullException(nameof(obj));}List<string>?temp=null;foreach(IConfigurationSectionsectioninconfiguration.GetChildren()){switch(section.Key){case"DisableColors":{if(configuration["DisableColors"]isstringstringValue3){obj.DisableColors=ParseBool(stringValue3,()=>section.Path)!;}}break;case"Format":{if(configuration["Format"]isstringstringValue4){obj.Format=ParseConsoleLoggerFormat(stringValue4,()=>section.Path)!;}}break;case"FormatterName":{obj.FormatterName=configuration["FormatterName"]!;}break;case"IncludeScopes":{if(configuration["IncludeScopes"]isstringstringValue6){obj.IncludeScopes=ParseBool(stringValue6,()=>section.Path)!;}}break;case"LogToStandardErrorThreshold":{if(configuration["LogToStandardErrorThreshold"]isstringstringValue7){obj.LogToStandardErrorThreshold=ParseLogLevel(stringValue7,()=>section.Path)!;}}break;case"TimestampFormat":{obj.TimestampFormat=configuration["TimestampFormat"]!;}break;case"UseUtcTimestamp":{if(configuration["UseUtcTimestamp"]isstringstringValue9){obj.UseUtcTimestamp=ParseBool(stringValue9,()=>section.Path)!;}}break;case"QueueFullMode":{if(configuration["QueueFullMode"]isstringstringValue10){obj.QueueFullMode=ParseConsoleLoggerQueueFullMode(stringValue10,()=>section.Path)!;}}break;case"MaxQueueLength":{if(configuration["MaxQueueLength"]isstringstringValue11){obj.MaxQueueLength=ParseInt(stringValue11,()=>section.Path)!;}}break;default:{if(binderOptions?.ErrorOnUnknownConfiguration==true){(temp??=newList<string>()).Add($"'{section.Key}'");}}break;}}if(tempis not null){thrownewInvalidOperationException($"'ErrorOnUnknownConfiguration' was set on the provided BinderOptions, but the following properties were not found on the instance of {typeof(ConsoleLoggerOptions)}: {string.Join(", ",temp)}");}}publicstaticvoidBindCore(IConfigurationconfiguration,refJsonWriterOptionsobj,BinderOptions?binderOptions){List<string>?temp=null;foreach(IConfigurationSectionsectioninconfiguration.GetChildren()){switch(section.Key){case"Encoder":{thrownewInvalidOperationException("Cannot create instance of type 'System.Text.Encodings.Web.JavaScriptEncoder' because it is missing a public instance constructor.");}case"Indented":{if(configuration["Indented"]isstringstringValue12){obj.Indented=ParseBool(stringValue12,()=>section.Path)!;}}break;case"MaxDepth":{if(configuration["MaxDepth"]isstringstringValue13){obj.MaxDepth=ParseInt(stringValue13,()=>section.Path)!;}}break;case"SkipValidation":{if(configuration["SkipValidation"]isstringstringValue14){obj.SkipValidation=ParseBool(stringValue14,()=>section.Path)!;}}break;default:{if(binderOptions?.ErrorOnUnknownConfiguration==true){(temp??=newList<string>()).Add($"'{section.Key}'");}}break;}}if(tempis not null){thrownewInvalidOperationException($"'ErrorOnUnknownConfiguration' was set on the provided BinderOptions, but the following properties were not found on the instance of {typeof(JsonWriterOptions)}: {string.Join(", ",temp)}");}}publicstaticvoidBindCore(IConfigurationconfiguration,refJsonConsoleFormatterOptionsobj,BinderOptions?binderOptions){if(objisnull){thrownewArgumentNullException(nameof(obj));}List<string>?temp=null;foreach(IConfigurationSectionsectioninconfiguration.GetChildren()){switch(section.Key){case"JsonWriterOptions":{if(HasChildren(section)){JsonWriterOptionstemp15=obj.JsonWriterOptions;BindCore(section,reftemp15,binderOptions);obj.JsonWriterOptions=temp15;}}break;case"IncludeScopes":{if(configuration["IncludeScopes"]isstringstringValue16){obj.IncludeScopes=ParseBool(stringValue16,()=>section.Path)!;}}break;case"TimestampFormat":{obj.TimestampFormat=configuration["TimestampFormat"]!;}break;case"UseUtcTimestamp":{if(configuration["UseUtcTimestamp"]isstringstringValue18){obj.UseUtcTimestamp=ParseBool(stringValue18,()=>section.Path)!;}}break;default:{if(binderOptions?.ErrorOnUnknownConfiguration==true){(temp??=newList<string>()).Add($"'{section.Key}'");}}break;}}if(tempis not null){thrownewInvalidOperationException($"'ErrorOnUnknownConfiguration' was set on the provided BinderOptions, but the following properties were not found on the instance of {typeof(JsonConsoleFormatterOptions)}: {string.Join(", ",temp)}");}}publicstaticvoidBindCore(IConfigurationconfiguration,refSimpleConsoleFormatterOptionsobj,BinderOptions?binderOptions){if(objisnull){thrownewArgumentNullException(nameof(obj));}List<string>?temp=null;foreach(IConfigurationSectionsectioninconfiguration.GetChildren()){switch(section.Key){case"ColorBehavior":{if(configuration["ColorBehavior"]isstringstringValue19){obj.ColorBehavior=ParseLoggerColorBehavior(stringValue19,()=>section.Path)!;}}break;case"SingleLine":{if(configuration["SingleLine"]isstringstringValue20){obj.SingleLine=ParseBool(stringValue20,()=>section.Path)!;}}break;case"IncludeScopes":{if(configuration["IncludeScopes"]isstringstringValue21){obj.IncludeScopes=ParseBool(stringValue21,()=>section.Path)!;}}break;case"TimestampFormat":{obj.TimestampFormat=configuration["TimestampFormat"]!;}break;case"UseUtcTimestamp":{if(configuration["UseUtcTimestamp"]isstringstringValue23){obj.UseUtcTimestamp=ParseBool(stringValue23,()=>section.Path)!;}}break;default:{if(binderOptions?.ErrorOnUnknownConfiguration==true){(temp??=newList<string>()).Add($"'{section.Key}'");}}break;}}if(tempis not null){thrownewInvalidOperationException($"'ErrorOnUnknownConfiguration' was set on the provided BinderOptions, but the following properties were not found on the instance of {typeof(SimpleConsoleFormatterOptions)}: {string.Join(", ",temp)}");}}publicstaticboolHasChildren(IConfigurationconfiguration){foreach(IConfigurationSectionsectioninconfiguration.GetChildren()){returntrue;}returnfalse;}publicstaticboolParseBool(stringstringValue,Func<string?>getPath){try{returnbool.Parse(stringValue);}catch(Exceptionexception){thrownewInvalidOperationException($"Failed to convert configuration value at '{getPath()}' to type '{typeof(bool)}'.",exception);}}publicstaticConsoleLoggerFormatParseConsoleLoggerFormat(stringstringValue,Func<string?>getPath){try{return(ConsoleLoggerFormat)Enum.Parse(typeof(ConsoleLoggerFormat),stringValue,ignoreCase:true);}catch(Exceptionexception){thrownewInvalidOperationException($"Failed to convert configuration value at '{getPath()}' to type '{typeof(ConsoleLoggerFormat)}'.",exception);}}publicstaticLogLevelParseLogLevel(stringstringValue,Func<string?>getPath){try{return(LogLevel)Enum.Parse(typeof(LogLevel),stringValue,ignoreCase:true);}catch(Exceptionexception){thrownewInvalidOperationException($"Failed to convert configuration value at '{getPath()}' to type '{typeof(LogLevel)}'.",exception);}}publicstaticConsoleLoggerQueueFullModeParseConsoleLoggerQueueFullMode(stringstringValue,Func<string?>getPath){try{return(ConsoleLoggerQueueFullMode)Enum.Parse(typeof(ConsoleLoggerQueueFullMode),stringValue,ignoreCase:true);}catch(Exceptionexception){thrownewInvalidOperationException($"Failed to convert configuration value at '{getPath()}' to type '{typeof(ConsoleLoggerQueueFullMode)}'.",exception);}}publicstaticintParseInt(stringstringValue,Func<string?>getPath){try{returnint.Parse(stringValue,NumberStyles.Integer,CultureInfo.InvariantCulture);}catch(Exceptionexception){thrownewInvalidOperationException($"Failed to convert configuration value at '{getPath()}' to type '{typeof(int)}'.",exception);}}publicstaticLoggerColorBehaviorParseLoggerColorBehavior(stringstringValue,Func<string?>getPath){try{return(LoggerColorBehavior)Enum.Parse(typeof(LoggerColorBehavior),stringValue,ignoreCase:true);}catch(Exceptionexception){thrownewInvalidOperationException($"Failed to convert configuration value at '{getPath()}' to type '{typeof(LoggerColorBehavior)}'.",exception);}}}}Details
|
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
3ae119c to
e472927Comparelayomia
commented
Jun 28, 2023
The failing test is due to #88112. |
Uh oh!
There was an error while loading. Please reload this page.
eerhardt
commented
Jun 28, 2023
That was totally a mistake in the test to have different cases. 😊 But at least now we know we have decent test coverage. |
If you wanted to unblock this you could fix that test to have matching case, the have the fix for #88112 add test case(s). |
layomia
commented
Jun 29, 2023
Taking Eric's (St. J :) suggestion - #88067 (comment). |
Uh oh!
There was an error while loading. Please reload this page.
Generated code following #88338 (click to view)```C# // #nullable enable #pragma warning disable CS0612, CS0618 // Suppress warnings about [Obsolete] member usage in generated code./// Generated helper providing an AOT and linking compatible implementation for configuration binding.[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Configuration.Binder.SourceGeneration", "42.42.42.42")] internal static class GeneratedConfigurationBinder { /// Attempts to bind the given object instance to configuration values by matching property names against configuration keys recursively.public static void Bind(this global::Microsoft.Extensions.Configuration.IConfiguration configuration, global::Microsoft.Extensions.Logging.Console.ConsoleFormatterOptions obj) => global::Microsoft.Extensions.Configuration.Binder.SourceGeneration.CoreBindingHelper.BindCore(configuration, ref obj, binderOptions: null); } namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration } |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
This reverts commit fa73b4edcd4a67ad195ab30a405e7f316ae34e5e.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Co-authored-by: Eric Erhardt <eric.erhardt@microsoft.com>
eiriktsarpalis
left a comment
There was a problem hiding this comment.
Great to see all this bespoke code being removed 👍
layomia
commented
Jul 14, 2023
Failing checks unrelated; check with running indicator appears to be complete -https://dev.azure.com/dnceng-public/public/_build/results?buildId=339049&view=logs&jobId=f02b8cf9-dd4d-54fc-c292-2bb1d305b019. |
Generated code (click to view)
Details