-
Notifications
You must be signed in to change notification settings - Fork 799
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
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -214,6 +214,40 @@ bool TryConvertEnumerable(object value, Type type, Destructuring destructuring, | |
{ | ||
result = SequenceValue.Empty; | ||
} | ||
else if (list is Array a && a.Rank > 1) | ||
{ | ||
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."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
here? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]; | ||
|
There was a problem hiding this comment.
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.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IScalarConversionPolicy
has no access todestructuring
boolean argumentThere was a problem hiding this comment.
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 usingIScalarConversionPolicy
is the type of object inside the array, regardless of the dimensions of the array.