Skip to content

Commit 6de06e7

Browse files
authored
Fix LogWithNameAttribute on complex objects (#110)
1 parent cd08cf8 commit 6de06e7

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public class PersonalData
5555
public string? Name { get; set; }
5656
}
5757
```
58-
<sup><a href='/src/Destructurama.Attributed.Tests/LogWithNameAttributeTests.cs#L29-L35' title='Snippet source file'>snippet source</a> | <a href='#snippet-logwithname' title='Start of snippet'>anchor</a></sup>
58+
<sup><a href='/src/Destructurama.Attributed.Tests/LogWithNameAttributeTests.cs#L63-L69' title='Snippet source file'>snippet source</a> | <a href='#snippet-logwithname' title='Start of snippet'>anchor</a></sup>
5959
<!-- endSnippet -->
6060

6161
## 2. Ignoring a property

src/Destructurama.Attributed.Tests/LogWithNameAttributeTests.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,41 @@ public void AttributesAreConsultedWhenDestructuring(string name)
2626
literalValue.ShouldBe(name);
2727
}
2828

29+
// https://github.com/destructurama/attributed/issues/65
30+
[Test]
31+
public void Issue65()
32+
{
33+
var customized = new MessageBase
34+
{
35+
Context = new ContextClass(),
36+
};
37+
var evt = DelegatingSink.Execute(customized);
38+
var sv = (StructureValue)evt.Properties["Customized"];
39+
sv.Properties.Count.ShouldBe(1);
40+
sv.Properties[0].Name.ShouldBe("messageContext");
41+
var sv2 = sv.Properties[0].Value.ShouldBeOfType<StructureValue>();
42+
sv2.Properties.Count.ShouldBe(2);
43+
sv2.Properties[0].Name.ShouldBe("Foo");
44+
sv2.Properties[1].Name.ShouldBe("Bar");
45+
sv2.Properties[0].Value.LiteralValue().ShouldBe("MyFoo");
46+
sv2.Properties[1].Value.LiteralValue().ShouldBe("MyBar");
47+
}
48+
49+
public class MessageBase
50+
{
51+
[LogWithName("messageContext")]
52+
public ContextClass? Context { get; set; }
53+
}
54+
55+
public class ContextClass
56+
{
57+
public string Foo { get; set; } = "MyFoo";
58+
59+
public string Bar { get; set; } = "MyBar";
60+
61+
public override string ToString() => "ContextClass ToString Output";
62+
}
63+
2964
#region LogWithName
3065
public class PersonalData
3166
{

src/Destructurama.Attributed.Tests/Support/DelegatingSink.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,4 @@ public static LogEvent Execute(object obj, string messageTemplate = "Here is {@C
2828

2929
return evt;
3030
}
31-
3231
}

src/Destructurama.Attributed/Attributed/LogWithNameAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public LogWithNameAttribute(string newName)
3838
/// <inheritdoc/>
3939
public bool TryCreateLogEventProperty(string name, object? value, ILogEventPropertyValueFactory propertyValueFactory, [NotNullWhen(true)] out LogEventProperty? property)
4040
{
41-
property = new LogEventProperty(_newName, propertyValueFactory.CreatePropertyValue(value));
41+
property = new LogEventProperty(_newName, propertyValueFactory.CreatePropertyValue(value, destructureObjects: true));
4242
return true;
4343
}
4444
}

0 commit comments

Comments
 (0)