Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for multi-dimensional arrays #2035

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/Serilog/Capturing/PropertyValueConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,40 @@ bool TryConvertEnumerable(object value, Type type, Destructuring destructuring,
{
result = SequenceValue.Empty;
}
else if (list is Array a && a.Rank > 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this could be plugged in as an IScalarConversionPolicy instead? Since those are checked before this code is reached, we could avoid the extra condition here. We do this already to specialize conversion of byte arrays.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IScalarConversionPolicy has no access to

  1. _depthLimiter (can be solved)
  2. destructuring boolean argument

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion, we should not use IScalarConversionPolicy here, because the determining factor whether or not using IScalarConversionPolicy is the type of object inside the array, regardless of the dimensions of the array.

{
var rows = new LogEventPropertyValue[a.GetLength(0)];
if (a.Rank == 2)
{
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 < 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.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is not a usage error but just a limitation, perhaps we should just use

result null;
return false;

here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think this is a good candidate to get into 4.0; shall I make these final tweaks and merge this, @sungam3r?

}
result = new SequenceValue(rows);
}
else
{
var array = new LogEventPropertyValue[list.Count];
Expand Down
48 changes: 48 additions & 0 deletions test/Serilog.Tests/Core/LoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,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);
});

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
[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);
});

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
[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);
});

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

[Fact]
Expand Down
2 changes: 1 addition & 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'">net462;net48</TargetFrameworks>
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);
}
}