Skip to content

Commit

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

#pragma warning disable Serilog004 // Constant MessageTemplate verifier
Expand Down Expand Up @@ -328,10 +327,10 @@ public void Two_Dimensional_Array_Should_Be_Logger_As_Sequence()
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]");
Assert.Equal(1, evt.Properties.Count);
var arr = (SequenceValue)evt.Properties["Value"];
Assert.Equal(3, arr.Elements.Count);
Assert.Equal("[[a,b],[c,d],[e,f]]", arr.LiteralValue());
}

// https://github.com/serilog/serilog/issues/2019
Expand All @@ -344,10 +343,10 @@ public void Three_Dimensional_Array_Should_Be_Logger_As_Sequence()
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]");
Assert.Equal(1, evt.Properties.Count);
var arr = (SequenceValue)evt.Properties["Value"];
Assert.Equal(3, arr.Elements.Count);
Assert.Equal("[[[a],[b]],[[c],[d]],[[e],[f]]]", arr.LiteralValue());
}


Expand All @@ -361,9 +360,9 @@ public void Four_Dimensional_Array_Not_Supported()
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");
Assert.Equal(1, evt.Properties.Count);
var val = evt.Properties["Value"].LiteralValue();
Assert.Equal("Capturing the property value threw an exception: NotSupportedException", val);
}

#if FEATURE_ASYNCDISPOSABLE
Expand Down
1 change: 0 additions & 1 deletion test/Serilog.Tests/Serilog.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
<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

0 comments on commit 148e53c

Please sign in to comment.