Skip to content

Commit 6aa905a

Browse files
committed
Enable nullable reference types
1 parent 8736256 commit 6aa905a

32 files changed

+153
-160
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: 4 additions & 4 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,7 +225,7 @@ 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);
231231
var section = string.IsNullOrWhiteSpace(readerOptions.SectionName) ? configuration : configuration.GetSection(readerOptions.SectionName);

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: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ class ConfigurationReader : IConfigurationReader
2020
readonly IConfiguration _section;
2121
readonly IReadOnlyCollection<Assembly> _configurationAssemblies;
2222
readonly ResolutionContext _resolutionContext;
23-
readonly IConfigurationRoot _configurationRoot;
23+
readonly IConfigurationRoot? _configurationRoot;
2424

25-
public ConfigurationReader(IConfiguration 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);
@@ -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)
@@ -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: 6 additions & 6 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,12 +50,12 @@ 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
/// Called when a log level switch is created while reading the configuration.
@@ -65,9 +65,9 @@ public ConfigurationReaderOptions() : this(dependencyContext: null)
6565
/// <item>For minimum level override switches, the switch name is the (partial) namespace or type name of the override.</item>
6666
/// </list>
6767
/// </summary>
68-
public Action<string, LoggingLevelSwitch> OnLevelSwitchCreated { get; init; }
68+
public Action<string, LoggingLevelSwitch>? OnLevelSwitchCreated { get; init; }
6969

70-
internal Assembly[] Assemblies { get; }
71-
internal DependencyContext DependencyContext { get; }
70+
internal Assembly[]? Assemblies { get; }
71+
internal DependencyContext? DependencyContext { get; }
7272
internal ConfigurationAssemblySource? ConfigurationAssemblySource { get; }
7373
}

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())

src/Serilog.Settings.Configuration/Settings/Configuration/ResolutionContext.cs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ sealed class ResolutionContext
1111
{
1212
readonly IDictionary<string, LoggingLevelSwitch> _declaredLevelSwitches;
1313
readonly IDictionary<string, LoggingFilterSwitchProxy> _declaredFilterSwitches;
14-
readonly IConfiguration _appConfiguration;
14+
readonly IConfiguration? _appConfiguration;
1515

16-
public ResolutionContext(IConfiguration appConfiguration = null, ConfigurationReaderOptions readerOptions = null)
16+
public ResolutionContext(IConfiguration? appConfiguration = null, ConfigurationReaderOptions? readerOptions = null)
1717
{
1818
_declaredLevelSwitches = new Dictionary<string, LoggingLevelSwitch>();
1919
_declaredFilterSwitches = new Dictionary<string, LoggingFilterSwitchProxy>();
@@ -51,18 +51,7 @@ public LoggingFilterSwitchProxy LookUpFilterSwitchByName(string switchName)
5151

5252
public bool HasAppConfiguration => _appConfiguration != null;
5353

54-
public IConfiguration AppConfiguration
55-
{
56-
get
57-
{
58-
if (!HasAppConfiguration)
59-
{
60-
throw new InvalidOperationException("AppConfiguration is not available");
61-
}
62-
63-
return _appConfiguration;
64-
}
65-
}
54+
public IConfiguration AppConfiguration => _appConfiguration ?? throw new InvalidOperationException("AppConfiguration is not available");
6655

6756
public string AddLevelSwitch(string levelSwitchName, LoggingLevelSwitch levelSwitch)
6857
{

src/Serilog.Settings.Configuration/Settings/Configuration/StringArgumentValue.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Reflection;
1+
using System.Diagnostics.CodeAnalysis;
2+
using System.Reflection;
23
using System.Text.RegularExpressions;
34

45
using Serilog.Core;
@@ -23,7 +24,7 @@ public StringArgumentValue(string providedValue)
2324
{ typeof(Type), s => Type.GetType(s, throwOnError:true) },
2425
};
2526

26-
public object ConvertTo(Type toType, ResolutionContext resolutionContext)
27+
public object? ConvertTo(Type toType, ResolutionContext resolutionContext)
2728
{
2829
var argumentValue = Environment.ExpandEnvironmentVariables(_providedValue);
2930

@@ -158,7 +159,7 @@ public object ConvertTo(Type toType, ResolutionContext resolutionContext)
158159
return Convert.ChangeType(argumentValue, toType, resolutionContext.ReaderOptions.FormatProvider);
159160
}
160161

161-
internal static Type FindType(string typeName)
162+
internal static Type? FindType(string typeName)
162163
{
163164
var type = Type.GetType(typeName);
164165
if (type == null)
@@ -172,7 +173,7 @@ internal static Type FindType(string typeName)
172173
return type;
173174
}
174175

175-
internal static bool TryParseStaticMemberAccessor(string input, out string accessorTypeName, out string memberName)
176+
internal static bool TryParseStaticMemberAccessor(string input, [NotNullWhen(true)] out string? accessorTypeName, [NotNullWhen(true)] out string? memberName)
176177
{
177178
if (input == null)
178179
{

src/Serilog.Settings.Configuration/Settings/Configuration/SurrogateConfigurationMethods.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ static LoggerConfiguration Sink(
4545
LoggerSinkConfiguration loggerSinkConfiguration,
4646
ILogEventSink sink,
4747
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
48-
LoggingLevelSwitch levelSwitch = null)
48+
LoggingLevelSwitch? levelSwitch = null)
4949
=> loggerSinkConfiguration.Sink(sink, restrictedToMinimumLevel, levelSwitch);
5050

5151
static LoggerConfiguration Logger(
5252
LoggerSinkConfiguration loggerSinkConfiguration,
5353
Action<LoggerConfiguration> configureLogger,
5454
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
55-
LoggingLevelSwitch levelSwitch = null)
55+
LoggingLevelSwitch? levelSwitch = null)
5656
=> loggerSinkConfiguration.Logger(configureLogger, restrictedToMinimumLevel, levelSwitch);
5757

5858
// .AuditTo...
@@ -61,14 +61,14 @@ static LoggerConfiguration Sink(
6161
LoggerAuditSinkConfiguration auditSinkConfiguration,
6262
ILogEventSink sink,
6363
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
64-
LoggingLevelSwitch levelSwitch = null)
64+
LoggingLevelSwitch? levelSwitch = null)
6565
=> auditSinkConfiguration.Sink(sink, restrictedToMinimumLevel, levelSwitch);
6666

6767
static LoggerConfiguration Logger(
6868
LoggerAuditSinkConfiguration auditSinkConfiguration,
6969
Action<LoggerConfiguration> configureLogger,
7070
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
71-
LoggingLevelSwitch levelSwitch = null)
71+
LoggingLevelSwitch? levelSwitch = null)
7272
=> auditSinkConfiguration.Logger(configureLogger, restrictedToMinimumLevel, levelSwitch);
7373

7474
// .Filter...
@@ -109,7 +109,7 @@ static LoggerConfiguration AtLevel(
109109
LoggerEnrichmentConfiguration loggerEnrichmentConfiguration,
110110
Action<LoggerEnrichmentConfiguration> configureEnricher,
111111
LogEventLevel enrichFromLevel = LevelAlias.Minimum,
112-
LoggingLevelSwitch levelSwitch = null)
112+
LoggingLevelSwitch? levelSwitch = null)
113113
=> levelSwitch != null ? loggerEnrichmentConfiguration.AtLevel(levelSwitch, configureEnricher)
114114
: loggerEnrichmentConfiguration.AtLevel(enrichFromLevel, configureEnricher);
115115

test/Serilog.Settings.Configuration.Tests/ConfigurationReaderTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public void CallableMethodsAreSelected()
154154
var suppliedArgumentNames = new[] { "pathFormat" };
155155

156156
var selected = ConfigurationReader.SelectConfigurationMethod(options, "DummyRollingFile", suppliedArgumentNames);
157-
Assert.Equal(typeof(string), selected.GetParameters()[1].ParameterType);
157+
Assert.Equal(typeof(string), selected?.GetParameters()[1].ParameterType);
158158
}
159159

160160
[Fact]
@@ -166,7 +166,7 @@ public void MethodsAreSelectedBasedOnCountOfMatchedArguments()
166166
var suppliedArgumentNames = new[] { "pathFormat", "formatter" };
167167

168168
var selected = ConfigurationReader.SelectConfigurationMethod(options, "DummyRollingFile", suppliedArgumentNames);
169-
Assert.Equal(typeof(ITextFormatter), selected.GetParameters()[1].ParameterType);
169+
Assert.Equal(typeof(ITextFormatter), selected?.GetParameters()[1].ParameterType);
170170
}
171171

172172
[Fact]
@@ -178,7 +178,7 @@ public void MethodsAreSelectedBasedOnCountOfMatchedArgumentsAndThenStringType()
178178
var suppliedArgumentNames = new[] { "pathFormat", "formatter" };
179179

180180
var selected = ConfigurationReader.SelectConfigurationMethod(options, "DummyRollingFile", suppliedArgumentNames);
181-
Assert.Equal(typeof(string), selected.GetParameters()[2].ParameterType);
181+
Assert.Equal(typeof(string), selected?.GetParameters()[2].ParameterType);
182182
}
183183

184184
public static IEnumerable<object[]> FlatMinimumLevel => new List<object[]>

0 commit comments

Comments
 (0)