Skip to content

Commit

Permalink
Add support for multi-dimensional arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
sungam3r authored and nblumhardt committed Oct 30, 2024
1 parent 7af255d commit 6e6507c
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
23 changes: 23 additions & 0 deletions src/Serilog/Capturing/PropertyValueConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,29 @@ IEnumerable<KeyValuePair<ScalarValue, LogEventPropertyValue>> MapToDictionaryEle
{
result = SequenceValue.Empty;
}
else if (list is Array a && a.Rank > 1)
{
int index = 0;
var array = new LogEventPropertyValue[list.Count];
if (a.Rank == 2)
{
for (int i = 0; i < a.GetLength(0); ++i)
for (int j = 0; j < a.GetLength(1); ++j)
array[index++] = _depthLimiter.CreatePropertyValue(a.GetValue(i, j), destructuring);
}
else if (a.Rank == 3)
{
for (int i = 0; i < a.GetLength(0); ++i)
for (int j = 0; j < a.GetLength(1); ++j)
for (int k = 0; k < a.GetLength(2); ++k)
array[index++] = _depthLimiter.CreatePropertyValue(a.GetValue(i, j, k), destructuring);
}
else
{
throw new NotSupportedException("Serilog does not support multi-dimensional arrays with Rank > 3.");
}
result = new SequenceValue(array);
}
else
{
var array = new LogEventPropertyValue[list.Count];
Expand Down
49 changes: 49 additions & 0 deletions test/Serilog.Tests/Core/LoggerTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Shouldly;
using System.Diagnostics;

#pragma warning disable Serilog004 // Constant MessageTemplate verifier
Expand Down Expand Up @@ -317,6 +318,54 @@ public void NullMessageTemplateParametersDoNotBreakBinding()
// ReSharper restore StructuredMessageTemplateProblem
}

// https://github.com/serilog/serilog/issues/2019
[Fact]
public void Two_Dimensional_Array_Should_Be_Logger_As_Sequence()
{
var evt = DelegatingSink.GetLogEvent(l =>
{
var a = new object[3, 2] { { "a", "b" }, { "c", "d" }, { "e", "f" } };
l.Error("{@Value}", a);
});

evt.Properties.Count.ShouldBe(1);
var arr = evt.Properties["Value"].ShouldBeOfType<SequenceValue>();
arr.Elements.Count.ShouldBe(6);
arr.LiteralValue().ShouldBe("[a,b,c,d,e,f]");
}

// https://github.com/serilog/serilog/issues/2019
[Fact]
public void Three_Dimensional_Array_Should_Be_Logger_As_Sequence()
{
var evt = DelegatingSink.GetLogEvent(l =>
{
var a = new object[3, 2, 1] { { { "a" }, { "b" } }, { { "c" }, { "d" } }, { { "e" }, { "f" } } };
l.Error("{@Value}", a);
});

evt.Properties.Count.ShouldBe(1);
var arr = evt.Properties["Value"].ShouldBeOfType<SequenceValue>();
arr.Elements.Count.ShouldBe(6);
arr.LiteralValue().ShouldBe("[a,b,c,d,e,f]");
}


// https://github.com/serilog/serilog/issues/2019
[Fact]
public void Four_Dimensional_Array_Not_Supported()
{
var evt = DelegatingSink.GetLogEvent(l =>
{
var a = new object[4, 3, 2, 1];
l.Error("{@Value}", a);
});

evt.Properties.Count.ShouldBe(1);
var val = evt.Properties["Value"].ShouldBeOfType<ScalarValue>();
val.LiteralValue().ShouldBe("Capturing the property value threw an exception: NotSupportedException");
}

#if FEATURE_ASYNCDISPOSABLE

[Fact]
Expand Down
3 changes: 2 additions & 1 deletion test/Serilog.Tests/Serilog.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<!-- Tests target .NET Framework 4.6.2 plus the latest RTM of .NET Framework -->
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT'">net48;net462</TargetFrameworks>
Expand All @@ -20,6 +20,7 @@
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1" PrivateAssets="all" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
<PackageReference Include="Shouldly" Version="4.1.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net462' or '$(TargetFramework)' == 'net48' ">
Expand Down
7 changes: 6 additions & 1 deletion test/Serilog.Tests/Support/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ public static class Extensions
{
public static object? LiteralValue(this LogEventPropertyValue @this)
{
return ((ScalarValue)@this).Value;
if (@this is ScalarValue scalar)
return scalar.Value;
else if (@this is SequenceValue sequence)
return $"[{string.Join(",", sequence.Elements.Select(e => e.LiteralValue()))}]";
else
throw new NotSupportedException(@this.GetType().Name);
}
}

0 comments on commit 6e6507c

Please sign in to comment.