From 5da0ec9740d08810818f9444384fedeaf4ae1fbd Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Fri, 29 Jul 2022 09:16:01 +1000 Subject: [PATCH] ScalarValue. value can be null (#1713) --- src/Serilog/Events/ScalarValue.cs | 22 +++++++++---------- test/Serilog.PerformanceTests/Support/Some.cs | 2 +- .../Capturing/PropertyValueConverterTests.cs | 10 ++++----- test/Serilog.Tests/Support/Extensions.cs | 2 +- .../Support/LogEventPropertyValueComparer.cs | 10 +++++++++ 5 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/Serilog/Events/ScalarValue.cs b/src/Serilog/Events/ScalarValue.cs index 8220de02d..610e9df62 100644 --- a/src/Serilog/Events/ScalarValue.cs +++ b/src/Serilog/Events/ScalarValue.cs @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +#nullable enable using System; using System.Globalization; using System.IO; @@ -28,7 +29,7 @@ public class ScalarValue : LogEventPropertyValue /// value. /// /// The value, which may be null. - public ScalarValue(object value) + public ScalarValue(object? value) { Value = value; } @@ -36,7 +37,7 @@ public ScalarValue(object value) /// /// The value, which may be null. /// - public object Value { get; } + public object? Value { get; } /// /// Render the value to the output. @@ -46,13 +47,13 @@ public ScalarValue(object value) /// A format provider to apply to the value, or null to use the default. /// . /// When is null - public override void Render(TextWriter output, string format = null, IFormatProvider formatProvider = null) + public override void Render(TextWriter output, string? format = null, IFormatProvider? formatProvider = null) { Render(Value, output, format, formatProvider); } /// When is null - internal static void Render(object value, TextWriter output, string format = null, IFormatProvider formatProvider = null) + internal static void Render(object? value, TextWriter output, string? format = null, IFormatProvider? formatProvider = null) { if (output == null) throw new ArgumentNullException(nameof(output)); @@ -77,14 +78,11 @@ internal static void Render(object value, TextWriter output, string format = nul return; } - if (formatProvider != null) + var custom = (ICustomFormatter?) formatProvider?.GetFormat(typeof(ICustomFormatter)); + if (custom != null) { - var custom = (ICustomFormatter)formatProvider.GetFormat(typeof(ICustomFormatter)); - if (custom != null) - { - output.Write(custom.Format(format, value, formatProvider)); - return; - } + output.Write(custom.Format(format, value, formatProvider)); + return; } if (value is IFormattable f) @@ -102,7 +100,7 @@ internal static void Render(object value, TextWriter output, string format = nul /// /// The instance to compare with. /// True if the instances are equal; otherwise, false. - public override bool Equals(object obj) + public override bool Equals(object? obj) { return obj is ScalarValue sv && Equals(Value, sv.Value); } diff --git a/test/Serilog.PerformanceTests/Support/Some.cs b/test/Serilog.PerformanceTests/Support/Some.cs index 2eb09c0c3..91a7eac33 100644 --- a/test/Serilog.PerformanceTests/Support/Some.cs +++ b/test/Serilog.PerformanceTests/Support/Some.cs @@ -11,7 +11,7 @@ public static LogEvent InformationEvent(string messageTemplate = "Hello, world!" #pragma warning disable Serilog004 // Constant MessageTemplate verifier logger.BindMessageTemplate(messageTemplate, propertyValues, out var parsedTemplate, out var boundProperties); #pragma warning restore Serilog004 // Constant MessageTemplate verifier - return new LogEvent(DateTime.Now, LogEventLevel.Information, null, parsedTemplate, boundProperties); + return new LogEvent(DateTime.Now, LogEventLevel.Information, null, parsedTemplate!, boundProperties!); } } } diff --git a/test/Serilog.Tests/Capturing/PropertyValueConverterTests.cs b/test/Serilog.Tests/Capturing/PropertyValueConverterTests.cs index 7ff038a70..31fc336c7 100644 --- a/test/Serilog.Tests/Capturing/PropertyValueConverterTests.cs +++ b/test/Serilog.Tests/Capturing/PropertyValueConverterTests.cs @@ -187,7 +187,7 @@ public void ByteArraysAreConvertedToStrings() { var bytes = Enumerable.Range(0, 10).Select(b => (byte)b).ToArray(); var pv = _converter.CreatePropertyValue(bytes); - var lv = (string)pv.LiteralValue(); + var lv = (string?)pv.LiteralValue(); Assert.Equal("00010203040506070809", lv); } @@ -196,7 +196,7 @@ public void ByteArraysLargerThan1kAreLimitedAndConvertedToStrings() { var bytes = Enumerable.Range(0, 1025).Select(b => (byte)b).ToArray(); var pv = _converter.CreatePropertyValue(bytes); - var lv = (string)pv.LiteralValue(); + var lv = (string?)pv.LiteralValue(); Assert.EndsWith("(1025 bytes)", lv); } @@ -206,7 +206,7 @@ public void ByteSpansAreConvertedToStrings() { var bytes = Enumerable.Range(0, 10).Select(b => (byte)b).ToArray().AsMemory(); var pv = _converter.CreatePropertyValue(bytes); - var lv = (string)pv.LiteralValue(); + var lv = (string?)pv.LiteralValue(); Assert.Equal("00010203040506070809", lv); } @@ -215,7 +215,7 @@ public void ByteSpansLargerThan1kAreLimitedAndConvertedToStrings() { var bytes = Enumerable.Range(0, 1025).Select(b => (byte)b).ToArray().AsMemory(); var pv = _converter.CreatePropertyValue(bytes); - var lv = (string)pv.LiteralValue(); + var lv = (string?)pv.LiteralValue(); Assert.EndsWith("(1025 bytes)", lv); } @@ -247,7 +247,7 @@ public void FailsGracefullyWhenAccessingPropertiesViaReflectionThrows() var l = sv.Properties.Single(m => m.Name == "Length"); Assert.Equal(3, l.Value.LiteralValue()); var k = sv.Properties.Single(m => m.Name == "IsEmpty"); - Assert.False((bool)k.Value.LiteralValue()); + Assert.False((bool?)k.Value.LiteralValue()); var s = sv.Properties.Single(m => m.Name == "Span"); } #endif diff --git a/test/Serilog.Tests/Support/Extensions.cs b/test/Serilog.Tests/Support/Extensions.cs index b4fa64067..870fca4cf 100644 --- a/test/Serilog.Tests/Support/Extensions.cs +++ b/test/Serilog.Tests/Support/Extensions.cs @@ -4,7 +4,7 @@ namespace Serilog.Tests.Support { public static class Extensions { - public static object LiteralValue(this LogEventPropertyValue @this) + public static object? LiteralValue(this LogEventPropertyValue @this) { return ((ScalarValue)@this).Value; } diff --git a/test/Serilog.Tests/Support/LogEventPropertyValueComparer.cs b/test/Serilog.Tests/Support/LogEventPropertyValueComparer.cs index c4d84ceac..d79b7eb3f 100644 --- a/test/Serilog.Tests/Support/LogEventPropertyValueComparer.cs +++ b/test/Serilog.Tests/Support/LogEventPropertyValueComparer.cs @@ -18,6 +18,16 @@ public bool Equals(LogEventPropertyValue? x, LogEventPropertyValue? y) { if (x is ScalarValue scalarX && y is ScalarValue scalarY) { + if (scalarX.Value is null && scalarY.Value is null) + { + return true; + } + + if (scalarX.Value is null || scalarY.Value is null) + { + return false; + } + return _objectEqualityComparer.Equals(scalarX.Value, scalarY.Value); }