Skip to content

Commit d776fdc

Browse files
committed
Merge branch 'dev' into internal
# Conflicts: # test/Serilog.Settings.Configuration.Tests/Serilog.Settings.Configuration.approved.txt
2 parents 8e2d9a9 + de58300 commit d776fdc

33 files changed

+249
-179
lines changed

Directory.Build.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)assets/Serilog.snk</AssemblyOriginatorKeyFile>
77
<ImplicitUsings>enable</ImplicitUsings>
88
<CheckEolTargetFramework>false</CheckEolTargetFramework>
9+
<Nullable>enable</Nullable>
910
</PropertyGroup>
1011
</Project>

sample/Sample/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,14 @@ public bool IsEnabled(LogEvent logEvent)
7373

7474
public class LoginData
7575
{
76-
public string Username;
76+
public string? Username;
7777
// ReSharper disable once NotAccessedField.Global
78-
public string Password;
78+
public string? Password;
7979
}
8080

8181
public class CustomPolicy : IDestructuringPolicy
8282
{
83-
public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue result)
83+
public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue? result)
8484
{
8585
result = null;
8686

src/Serilog.Settings.Configuration/ConfigurationLoggerConfigurationExtensions.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static LoggerConfiguration Configuration(
4747
this LoggerSettingsConfiguration settingConfiguration,
4848
IConfiguration configuration,
4949
string sectionName,
50-
DependencyContext dependencyContext = null)
50+
DependencyContext? dependencyContext = null)
5151
{
5252
if (settingConfiguration == null) throw new ArgumentNullException(nameof(settingConfiguration));
5353
if (configuration == null) throw new ArgumentNullException(nameof(configuration));
@@ -87,7 +87,7 @@ public static LoggerConfiguration Configuration(
8787
public static LoggerConfiguration ConfigurationSection(
8888
this LoggerSettingsConfiguration settingConfiguration,
8989
IConfigurationSection configSection,
90-
DependencyContext dependencyContext = null)
90+
DependencyContext? dependencyContext = null)
9191
{
9292
if (settingConfiguration == null) throw new ArgumentNullException(nameof(settingConfiguration));
9393
if (configSection == null) throw new ArgumentNullException(nameof(configSection));
@@ -214,7 +214,7 @@ public static LoggerConfiguration Configuration(
214214
public static LoggerConfiguration Configuration(
215215
this LoggerSettingsConfiguration settingConfiguration,
216216
IConfiguration configuration,
217-
ConfigurationReaderOptions readerOptions = null)
217+
ConfigurationReaderOptions? readerOptions = null)
218218
{
219219
var configurationReader = readerOptions switch
220220
{
@@ -225,23 +225,23 @@ public static LoggerConfiguration Configuration(
225225
return settingConfiguration.Settings(configurationReader);
226226
}
227227

228-
static ConfigurationReader GetConfigurationReader(IConfiguration configuration, ConfigurationReaderOptions readerOptions, DependencyContext dependencyContext)
228+
static ConfigurationReader GetConfigurationReader(IConfiguration configuration, ConfigurationReaderOptions readerOptions, DependencyContext? dependencyContext)
229229
{
230230
var assemblyFinder = dependencyContext == null ? AssemblyFinder.Auto() : AssemblyFinder.ForDependencyContext(dependencyContext);
231-
var section = configuration.GetSection(readerOptions.SectionName);
231+
var section = string.IsNullOrWhiteSpace(readerOptions.SectionName) ? configuration : configuration.GetSection(readerOptions.SectionName);
232232
return new ConfigurationReader(section, assemblyFinder, readerOptions, configuration);
233233
}
234234

235235
static ConfigurationReader GetConfigurationReader(IConfiguration configuration, ConfigurationReaderOptions readerOptions, ConfigurationAssemblySource source)
236236
{
237237
var assemblyFinder = AssemblyFinder.ForSource(source);
238-
var section = configuration.GetSection(readerOptions.SectionName);
238+
var section = string.IsNullOrWhiteSpace(readerOptions.SectionName) ? configuration : configuration.GetSection(readerOptions.SectionName);
239239
return new ConfigurationReader(section, assemblyFinder, readerOptions, configuration);
240240
}
241241

242242
static ConfigurationReader GetConfigurationReader(IConfiguration configuration, ConfigurationReaderOptions readerOptions, IReadOnlyCollection<Assembly> assemblies)
243243
{
244-
var section = configuration.GetSection(readerOptions.SectionName);
244+
var section = string.IsNullOrWhiteSpace(readerOptions.SectionName) ? configuration : configuration.GetSection(readerOptions.SectionName);
245245
return new ConfigurationReader(section, assemblies, new ResolutionContext(configuration, readerOptions));
246246
}
247247
}

src/Serilog.Settings.Configuration/Settings/Configuration/Assemblies/AssemblyFinder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ abstract class AssemblyFinder
77
{
88
public abstract IReadOnlyList<AssemblyName> FindAssembliesContainingName(string nameToFind);
99

10-
protected static bool IsCaseInsensitiveMatch(string text, string textToFind)
10+
protected static bool IsCaseInsensitiveMatch(string? text, string textToFind)
1111
{
1212
return text != null && text.ToLowerInvariant().Contains(textToFind.ToLowerInvariant());
1313
}

src/Serilog.Settings.Configuration/Settings/Configuration/Assemblies/DllScanningAssemblyFinder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ where IsCaseInsensitiveMatch(assemblyFileName, nameToFind)
4646

4747
return query.ToList().AsReadOnly();
4848

49-
static AssemblyName TryGetAssemblyNameFrom(string path)
49+
static AssemblyName? TryGetAssemblyNameFrom(string path)
5050
{
5151
try
5252
{

src/Serilog.Settings.Configuration/Settings/Configuration/ConfigurationReader.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ class ConfigurationReader : IConfigurationReader
1717
{
1818
const string LevelSwitchNameRegex = @"^\${0,1}[A-Za-z]+[A-Za-z0-9]*$";
1919

20-
readonly IConfigurationSection _section;
20+
readonly IConfiguration _section;
2121
readonly IReadOnlyCollection<Assembly> _configurationAssemblies;
2222
readonly ResolutionContext _resolutionContext;
23-
readonly IConfigurationRoot _configurationRoot;
23+
readonly IConfigurationRoot? _configurationRoot;
2424

25-
public ConfigurationReader(IConfigurationSection configSection, AssemblyFinder assemblyFinder, ConfigurationReaderOptions readerOptions, IConfiguration configuration = null)
25+
public ConfigurationReader(IConfiguration configSection, AssemblyFinder assemblyFinder, ConfigurationReaderOptions readerOptions, IConfiguration? configuration = null)
2626
{
2727
_section = configSection ?? throw new ArgumentNullException(nameof(configSection));
2828
_configurationAssemblies = LoadConfigurationAssemblies(_section, assemblyFinder);
@@ -31,7 +31,7 @@ public ConfigurationReader(IConfigurationSection configSection, AssemblyFinder a
3131
}
3232

3333
// Used internally for processing nested configuration sections -- see GetMethodCalls below.
34-
internal ConfigurationReader(IConfigurationSection configSection, IReadOnlyCollection<Assembly> configurationAssemblies, ResolutionContext resolutionContext)
34+
internal ConfigurationReader(IConfiguration configSection, IReadOnlyCollection<Assembly> configurationAssemblies, ResolutionContext resolutionContext)
3535
{
3636
_section = configSection ?? throw new ArgumentNullException(nameof(configSection));
3737
_configurationAssemblies = configurationAssemblies ?? throw new ArgumentNullException(nameof(configurationAssemblies));
@@ -145,8 +145,8 @@ void ApplyMinimumLevel(LoggerConfiguration loggerConfiguration)
145145
{
146146
var minimumLevelDirective = _section.GetSection("MinimumLevel");
147147

148-
IConfigurationSection defaultMinLevelDirective = GetDefaultMinLevelDirective();
149-
if (defaultMinLevelDirective.Value != null)
148+
IConfigurationSection? defaultMinLevelDirective = GetDefaultMinLevelDirective();
149+
if (defaultMinLevelDirective?.Value != null)
150150
{
151151
ApplyMinimumLevelConfiguration(defaultMinLevelDirective, (configuration, levelSwitch) => configuration.ControlledBy(levelSwitch));
152152
}
@@ -189,7 +189,7 @@ void ApplyMinimumLevelConfiguration(IConfigurationSection directive, Action<Logg
189189
SubscribeToLoggingLevelChanges(directive, levelSwitch);
190190
}
191191

192-
IConfigurationSection GetDefaultMinLevelDirective()
192+
IConfigurationSection? GetDefaultMinLevelDirective()
193193
{
194194
var defaultLevelDirective = minimumLevelDirective.GetSection("Default");
195195
if (_configurationRoot != null && minimumLevelDirective.Value != null && defaultLevelDirective.Value != null)
@@ -298,7 +298,7 @@ void ApplyEnrichment(LoggerConfiguration loggerConfiguration)
298298
}
299299
}
300300

301-
internal ILookup<string, Dictionary<string, IConfigurationArgumentValue>> GetMethodCalls(IConfigurationSection directive)
301+
internal ILookup<string, Dictionary<string, IConfigurationArgumentValue>> GetMethodCalls(IConfiguration directive)
302302
{
303303
var children = directive.GetChildren().ToList();
304304

@@ -356,7 +356,7 @@ internal static IConfigurationArgumentValue GetArgumentValue(IConfigurationSecti
356356
return argumentValue;
357357
}
358358

359-
static IReadOnlyCollection<Assembly> LoadConfigurationAssemblies(IConfigurationSection section, AssemblyFinder assemblyFinder)
359+
static IReadOnlyCollection<Assembly> LoadConfigurationAssemblies(IConfiguration section, AssemblyFinder assemblyFinder)
360360
{
361361
var serilogAssembly = typeof(ILogger).Assembly;
362362
var assemblies = new Dictionary<string, Assembly> { [serilogAssembly.FullName] = serilogAssembly };
@@ -413,7 +413,7 @@ static bool HasImplicitValueWhenNotSpecified(ParameterInfo paramInfo)
413413
|| paramInfo.ParameterType == typeof(IConfiguration);
414414
}
415415

416-
object GetImplicitValueForNotSpecifiedKey(ParameterInfo parameter, MethodInfo methodToInvoke)
416+
object? GetImplicitValueForNotSpecifiedKey(ParameterInfo parameter, MethodInfo methodToInvoke)
417417
{
418418
if (!HasImplicitValueWhenNotSpecified(parameter))
419419
{
@@ -439,7 +439,7 @@ object GetImplicitValueForNotSpecifiedKey(ParameterInfo parameter, MethodInfo me
439439
return parameter.DefaultValue;
440440
}
441441

442-
internal static MethodInfo SelectConfigurationMethod(IReadOnlyCollection<MethodInfo> candidateMethods, string name, IReadOnlyCollection<string> suppliedArgumentNames)
442+
internal static MethodInfo? SelectConfigurationMethod(IReadOnlyCollection<MethodInfo> candidateMethods, string name, IReadOnlyCollection<string> suppliedArgumentNames)
443443
{
444444
// Per issue #111, it is safe to use case-insensitive matching on argument names. The CLR doesn't permit this type
445445
// of overloading, and the Microsoft.Extensions.Configuration keys are case-insensitive (case is preserved with some

src/Serilog.Settings.Configuration/Settings/Configuration/ConfigurationReaderOptions.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public ConfigurationReaderOptions() : this(dependencyContext: null)
3838
/// The dependency context from which sink/enricher packages can be located. If <see langword="null"/>, the platform default will be used.
3939
/// </param>
4040
/// <remarks>Prefer the constructor taking explicit assemblies: <see cref="ConfigurationReaderOptions(System.Reflection.Assembly[])"/>. It's the only one supporting single-file publishing.</remarks>
41-
public ConfigurationReaderOptions(DependencyContext dependencyContext) => DependencyContext = dependencyContext;
41+
public ConfigurationReaderOptions(DependencyContext? dependencyContext) => DependencyContext = dependencyContext;
4242

4343
/// <summary>
4444
/// Initialize a new instance of the <see cref="ConfigurationReaderOptions"/> class.
@@ -50,22 +50,22 @@ public ConfigurationReaderOptions() : this(dependencyContext: null)
5050
/// <summary>
5151
/// The section name for section which contains a Serilog section. Defaults to <c>Serilog</c>.
5252
/// </summary>
53-
public string SectionName { get; init; } = ConfigurationLoggerConfigurationExtensions.DefaultSectionName;
53+
public string? SectionName { get; init; } = ConfigurationLoggerConfigurationExtensions.DefaultSectionName;
5454

5555
/// <summary>
5656
/// The <see cref="IFormatProvider"/> used when converting strings to other object types. Defaults to the invariant culture.
5757
/// </summary>
58-
public IFormatProvider FormatProvider { get; init; } = CultureInfo.InvariantCulture;
58+
public IFormatProvider? FormatProvider { get; init; } = CultureInfo.InvariantCulture;
5959

6060
/// <summary>
6161
/// Allows to use internal types for extension methods for sink configuration. Defaults to <see langword="false"/>.
6262
/// </summary>
63-
public bool AllowInternalTypes { get; set; }
63+
public bool AllowInternalTypes { get; init; }
6464

6565
/// <summary>
6666
/// Allows to use internal extension methods for sink configuration. Defaults to <see langword="false"/>.
6767
/// </summary>
68-
public bool AllowInternalMethods { get; set; }
68+
public bool AllowInternalMethods { get; init; }
6969

7070
/// <summary>
7171
/// Called when a log level switch is created while reading the configuration.
@@ -75,9 +75,9 @@ public ConfigurationReaderOptions() : this(dependencyContext: null)
7575
/// <item>For minimum level override switches, the switch name is the (partial) namespace or type name of the override.</item>
7676
/// </list>
7777
/// </summary>
78-
public Action<string, LoggingLevelSwitch> OnLevelSwitchCreated { get; init; }
78+
public Action<string, LoggingLevelSwitch>? OnLevelSwitchCreated { get; init; }
7979

80-
internal Assembly[] Assemblies { get; }
81-
internal DependencyContext DependencyContext { get; }
80+
internal Assembly[]? Assemblies { get; }
81+
internal DependencyContext? DependencyContext { get; }
8282
internal ConfigurationAssemblySource? ConfigurationAssemblySource { get; }
8383
}

src/Serilog.Settings.Configuration/Settings/Configuration/IConfigurationArgumentValue.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
interface IConfigurationArgumentValue
44
{
5-
object ConvertTo(Type toType, ResolutionContext resolutionContext);
5+
object? ConvertTo(Type toType, ResolutionContext resolutionContext);
66
}

src/Serilog.Settings.Configuration/Settings/Configuration/LoggingFilterSwitchProxy.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
class LoggingFilterSwitchProxy
44
{
5-
readonly Action<string> _setProxy;
6-
readonly Func<string> _getProxy;
5+
readonly Action<string?> _setProxy;
6+
readonly Func<string?> _getProxy;
77

88
LoggingFilterSwitchProxy(object realSwitch)
99
{
@@ -12,26 +12,26 @@ class LoggingFilterSwitchProxy
1212
var type = realSwitch.GetType();
1313
var expressionProperty = type.GetProperty("Expression") ?? throw new MissingMemberException(type.FullName, "Expression");
1414

15-
_setProxy = (Action<string>)Delegate.CreateDelegate(
16-
typeof(Action<string>),
15+
_setProxy = (Action<string?>)Delegate.CreateDelegate(
16+
typeof(Action<string?>),
1717
realSwitch,
1818
expressionProperty.GetSetMethod());
1919

20-
_getProxy = (Func<string>)Delegate.CreateDelegate(
21-
typeof(Func<string>),
20+
_getProxy = (Func<string?>)Delegate.CreateDelegate(
21+
typeof(Func<string?>),
2222
realSwitch,
2323
expressionProperty.GetGetMethod());
2424
}
2525

2626
public object RealSwitch { get; }
2727

28-
public string Expression
28+
public string? Expression
2929
{
3030
get => _getProxy();
3131
set => _setProxy(value);
3232
}
3333

34-
public static LoggingFilterSwitchProxy Create(string expression = null)
34+
public static LoggingFilterSwitchProxy? Create(string? expression = null)
3535
{
3636
var filterSwitchType =
3737
Type.GetType("Serilog.Expressions.LoggingFilterSwitch, Serilog.Expressions") ??

src/Serilog.Settings.Configuration/Settings/Configuration/ObjectArgumentValue.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Diagnostics.CodeAnalysis;
12
using System.Linq.Expressions;
23
using System.Reflection;
34

@@ -20,7 +21,7 @@ public ObjectArgumentValue(IConfigurationSection section, IReadOnlyCollection<As
2021
_configurationAssemblies = configurationAssemblies ?? throw new ArgumentNullException(nameof(configurationAssemblies));
2122
}
2223

23-
public object ConvertTo(Type toType, ResolutionContext resolutionContext)
24+
public object? ConvertTo(Type toType, ResolutionContext resolutionContext)
2425
{
2526
// return the entire section for internal processing
2627
if (toType == typeof(IConfigurationSection)) return _section;
@@ -71,7 +72,7 @@ object CreateArray()
7172
return array;
7273
}
7374

74-
bool TryCreateContainer(out object result)
75+
bool TryCreateContainer([NotNullWhen(true)] out object? result)
7576
{
7677
result = null;
7778

@@ -98,7 +99,7 @@ bool TryCreateContainer(out object result)
9899
}
99100

100101
internal static bool TryBuildCtorExpression(
101-
IConfigurationSection section, Type parameterType, ResolutionContext resolutionContext, out NewExpression ctorExpression)
102+
IConfigurationSection section, Type parameterType, ResolutionContext resolutionContext, [NotNullWhen(true)] out NewExpression? ctorExpression)
102103
{
103104
ctorExpression = null;
104105

@@ -138,11 +139,11 @@ internal static bool TryBuildCtorExpression(
138139
from p in c.GetParameters()
139140
let argumentBindResult = suppliedArguments.TryGetValue(p.Name, out var argValue) switch
140141
{
141-
true => new { success = true, hasMatch = true, value = (object)argValue },
142+
true => new { success = true, hasMatch = true, value = (object?)argValue },
142143
false => p.HasDefaultValue switch
143144
{
144-
true => new { success = true, hasMatch = false, value = p.DefaultValue },
145-
false => new { success = false, hasMatch = false, value = (object)null },
145+
true => new { success = true, hasMatch = false, value = (object?)p.DefaultValue },
146+
false => new { success = false, hasMatch = false, value = (object?)null },
146147
},
147148
}
148149
group new { argumentBindResult, p.ParameterType } by c into gr
@@ -178,7 +179,7 @@ where gr.All(z => z.argumentBindResult.success)
178179
ctorExpression = Expression.New(ctor.ConstructorInfo, ctorArguments);
179180
return true;
180181

181-
static bool TryBindToCtorArgument(object value, Type type, ResolutionContext resolutionContext, out Expression argumentExpression)
182+
static bool TryBindToCtorArgument(object value, Type type, ResolutionContext resolutionContext, [NotNullWhen(true)] out Expression? argumentExpression)
182183
{
183184
argumentExpression = null;
184185

@@ -217,7 +218,7 @@ static bool TryBindToCtorArgument(object value, Type type, ResolutionContext res
217218
}
218219
}
219220

220-
static bool IsContainer(Type type, out Type elementType)
221+
static bool IsContainer(Type type, [NotNullWhen(true)] out Type? elementType)
221222
{
222223
elementType = null;
223224
foreach (var iface in type.GetInterfaces())

0 commit comments

Comments
 (0)