Skip to content
This repository was archived by the owner on Feb 27, 2022. It is now read-only.

Commit 0fe672d

Browse files
authored
Merge pull request #32 from nblumhardt/lifting-error
Fix evaluation of filters that return structure or sequence values
2 parents d6d100c + 9e6609b commit 0fe672d

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/Serilog.Filters.Expressions/Filters/Expressions/Runtime/Representation.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ public static object Expose(object internalValue)
4444

4545
var sequence = internalValue as SequenceValue;
4646
if (sequence != null)
47-
return sequence.Elements.Select(Expose).ToArray();
47+
return sequence.Elements.Select(ExposeOrRepresent).ToArray();
4848

4949
var structure = internalValue as StructureValue;
5050
if (structure != null)
5151
{
52-
var r = structure.Properties.ToDictionary(p => p.Name, p => Expose(p.Value));
52+
var r = structure.Properties.ToDictionary(p => p.Name, p => ExposeOrRepresent(p.Value));
5353
if (structure.TypeTag != null)
5454
r["$type"] = structure.TypeTag;
5555
return r;
@@ -58,12 +58,23 @@ public static object Expose(object internalValue)
5858
var dictionary = internalValue as DictionaryValue;
5959
if (dictionary != null)
6060
{
61-
return dictionary.Elements.ToDictionary(p => Expose(p.Key), p => Expose(p.Value));
61+
return dictionary.Elements.ToDictionary(p => ExposeOrRepresent(p.Key), p => ExposeOrRepresent(p.Value));
6262
}
6363

6464
return internalValue;
6565
}
6666

67+
static object ExposeOrRepresent(object internalValue)
68+
{
69+
if (internalValue is Undefined)
70+
return null;
71+
72+
if (internalValue is ScalarValue sv)
73+
return Represent(sv);
74+
75+
return Expose(internalValue);
76+
}
77+
6778
public static LogEventPropertyValue Recapture(object value)
6879
{
6980
return value is LogEventPropertyValue lepv ? lepv : new ScalarValue(value);

test/Serilog.Filters.Expressions.Tests/FilterExpressionCompilerTests.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Serilog.Events;
1+
using System.Collections.Generic;
2+
using Serilog.Events;
23
using Serilog.Filters.Expressions.Tests.Support;
34
using System.Linq;
45
using Xunit;
@@ -150,5 +151,15 @@ static void AssertFiltering(string expression, LogEvent match, params LogEvent[]
150151
Assert.Single(sink.Events);
151152
Assert.Same(match, sink.Events.Single());
152153
}
154+
155+
[Fact]
156+
public void StructuresAreExposedAsDictionaries()
157+
{
158+
var evt = Some.InformationEvent("{@Person}", new { Name = "nblumhardt" });
159+
var expr = FilterLanguage.CreateFilter("Person");
160+
var val = expr(evt);
161+
var dict = Assert.IsType<Dictionary<string, object>>(val);
162+
Assert.Equal("nblumhardt", dict["Name"]);
163+
}
153164
}
154165
}

0 commit comments

Comments
 (0)