Skip to content

Commit

Permalink
ScalarValue. value can be null (serilog#1713)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored and Twinki14 committed Dec 30, 2023
1 parent 67ef72b commit 5da0ec9
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 19 deletions.
22 changes: 10 additions & 12 deletions src/Serilog/Events/ScalarValue.cs
Expand Up @@ -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;
Expand All @@ -28,15 +29,15 @@ public class ScalarValue : LogEventPropertyValue
/// value.
/// </summary>
/// <param name="value">The value, which may be <code>null</code>.</param>
public ScalarValue(object value)
public ScalarValue(object? value)
{
Value = value;
}

/// <summary>
/// The value, which may be <code>null</code>.
/// </summary>
public object Value { get; }
public object? Value { get; }

/// <summary>
/// Render the value to the output.
Expand All @@ -46,13 +47,13 @@ public ScalarValue(object value)
/// <param name="formatProvider">A format provider to apply to the value, or null to use the default.</param>
/// <seealso cref="LogEventPropertyValue.ToString(string, IFormatProvider)"/>.
/// <exception cref="ArgumentNullException">When <paramref name="output"/> is <code>null</code></exception>
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);
}

/// <exception cref="ArgumentNullException">When <paramref name="output"/> is <code>null</code></exception>
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));

Expand All @@ -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)
Expand All @@ -102,7 +100,7 @@ internal static void Render(object value, TextWriter output, string format = nul
/// </summary>
/// <param name="obj">The instance to compare with.</param>
/// <returns>True if the instances are equal; otherwise, false.</returns>
public override bool Equals(object obj)
public override bool Equals(object? obj)
{
return obj is ScalarValue sv && Equals(Value, sv.Value);
}
Expand Down
2 changes: 1 addition & 1 deletion test/Serilog.PerformanceTests/Support/Some.cs
Expand Up @@ -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!);
}
}
}
10 changes: 5 additions & 5 deletions test/Serilog.Tests/Capturing/PropertyValueConverterTests.cs
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/Serilog.Tests/Support/Extensions.cs
Expand Up @@ -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;
}
Expand Down
10 changes: 10 additions & 0 deletions test/Serilog.Tests/Support/LogEventPropertyValueComparer.cs
Expand Up @@ -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);
}

Expand Down

0 comments on commit 5da0ec9

Please sign in to comment.