Skip to content

Commit 8d9b11e

Browse files
Fix nullability warnings
1 parent 9d72586 commit 8d9b11e

File tree

4 files changed

+16
-10
lines changed

4 files changed

+16
-10
lines changed

src/Serilog.Enrichers.Sensitive/SensitiveArea.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ namespace Serilog.Enrichers.Sensitive
55
{
66
public class SensitiveArea : IDisposable
77
{
8-
private static readonly AsyncLocal<SensitiveArea> InstanceLocal = new AsyncLocal<SensitiveArea>();
8+
private static readonly AsyncLocal<SensitiveArea?> InstanceLocal = new();
99

10-
public static SensitiveArea Instance
10+
public static SensitiveArea? Instance
1111
{
1212
get => InstanceLocal.Value;
1313
set => InstanceLocal.Value = value;

src/Serilog.Enrichers.Sensitive/SensitiveDataEnricher.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ internal class SensitiveDataEnricher : ILogEventEnricher
2121
private readonly List<string> _excludeProperties;
2222

2323
public SensitiveDataEnricher(
24-
Action<SensitiveDataEnricherOptions> options)
24+
Action<SensitiveDataEnricherOptions>? options)
2525
{
2626
var enricherOptions = new SensitiveDataEnricherOptions();
2727

@@ -39,12 +39,19 @@ public SensitiveDataEnricher(
3939
_maskValue = enricherOptions.MaskValue;
4040
_maskProperties = enricherOptions.MaskProperties ?? new List<string>();
4141
_excludeProperties = enricherOptions.ExcludeProperties ?? new List<string>();
42+
_maskingOperators = enricherOptions.MaskingOperators.ToList();
4243

4344
var fields = typeof(LogEvent).GetFields(BindingFlags.Instance | BindingFlags.NonPublic);
4445

45-
_messageTemplateBackingField = fields.SingleOrDefault(f => f.Name.Contains("<MessageTemplate>"));
46+
var backingField = fields.SingleOrDefault(f => f.Name.Contains("<MessageTemplate>"));
47+
48+
if (backingField == null)
49+
{
50+
throw new InvalidOperationException(
51+
"Could not find the backing field for the message template on the LogEvent type");
52+
}
4653

47-
_maskingOperators = enricherOptions.MaskingOperators.ToList();
54+
_messageTemplateBackingField = backingField;
4855
}
4956

5057
public SensitiveDataEnricher(
@@ -83,7 +90,7 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
8390
.AddOrUpdateProperty(
8491
new LogEventProperty(
8592
property.Key,
86-
maskedValue));
93+
maskedValue!));
8794
}
8895
}
8996
}
@@ -125,7 +132,7 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
125132
if (wasMasked)
126133
{
127134
anyMasked = true;
128-
propList.Add(new LogEventProperty(prop.Name, maskedValue));
135+
propList.Add(new LogEventProperty(prop.Name, maskedValue!));
129136
}
130137
else
131138
{

test/Serilog.Enrichers.Sensitive.Tests.Unit/WhenMaskingWithCustomMaskValue.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class WhenMaskingWithCustomMaskValue
1212
public void GivenMaskValueIsNull_ArgumentNullExceptionIsThrown()
1313
{
1414
Action action = () => new LoggerConfiguration()
15-
.Enrich.WithSensitiveDataMasking(options => options.MaskValue = null)
15+
.Enrich.WithSensitiveDataMasking(options => options.MaskValue = null!)
1616
.CreateLogger();
1717

1818
action

test/Serilog.Enrichers.Sensitive.Tests.Unit/WhenMaskingWithRegexOperator.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Text.RegularExpressions;
32
using FluentAssertions;
43
using Xunit;
54

@@ -18,7 +17,7 @@ public RegexExtenderWithOptions(string regexPattern) : base(regexPattern)
1817
[Fact]
1918
public void GivenConstructor_NullPatternThrowsException()
2019
{
21-
var ex = Record.Exception(() => new RegexExtenderWithOptions(null));
20+
var ex = Record.Exception(() => new RegexExtenderWithOptions(null!));
2221
ex
2322
.Should()
2423
.NotBeNull()

0 commit comments

Comments
 (0)