Skip to content

Commit

Permalink
Fix LogWithNameAttribute on complex objects
Browse files Browse the repository at this point in the history
  • Loading branch information
sungam3r committed Feb 27, 2024
1 parent cd08cf8 commit 961d992
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class PersonalData
public string? Name { get; set; }
}
```
<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>
<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>
<!-- endSnippet -->

## 2. Ignoring a property
Expand Down
34 changes: 34 additions & 0 deletions src/Destructurama.Attributed.Tests/LogWithNameAttributeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,40 @@ public void AttributesAreConsultedWhenDestructuring(string name)
literalValue.ShouldBe(name);
}

[Test]
public void Issue65()
{
var customized = new MessageBase
{
Context = new ContextClass(),
};
var evt = DelegatingSink.Execute(customized);
var sv = (StructureValue)evt.Properties["Customized"];
sv.Properties.Count.ShouldBe(1);
sv.Properties[0].Name.ShouldBe("messageContext");
var sv2 = sv.Properties[0].Value.ShouldBeOfType<StructureValue>();
sv2.Properties.Count.ShouldBe(2);
sv2.Properties[0].Name.ShouldBe("Foo");
sv2.Properties[1].Name.ShouldBe("Bar");
sv2.Properties[0].Value.LiteralValue().ShouldBe("MyFoo");
sv2.Properties[1].Value.LiteralValue().ShouldBe("MyBar");
}

public class MessageBase
{
[LogWithName("messageContext")]
public ContextClass? Context { get; set; }
}

public class ContextClass
{
public string Foo { get; set; } = "MyFoo";

public string Bar { get; set; } = "MyBar";

public override string ToString() => "ContextClass ToString Output";
}

#region LogWithName
public class PersonalData
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,4 @@ public static LogEvent Execute(object obj, string messageTemplate = "Here is {@C

return evt;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public LogWithNameAttribute(string newName)
/// <inheritdoc/>
public bool TryCreateLogEventProperty(string name, object? value, ILogEventPropertyValueFactory propertyValueFactory, [NotNullWhen(true)] out LogEventProperty? property)
{
property = new LogEventProperty(_newName, propertyValueFactory.CreatePropertyValue(value));
property = new LogEventProperty(_newName, propertyValueFactory.CreatePropertyValue(value, destructureObjects: true));
return true;
}
}

1 comment on commit 961d992

@github-actions
Copy link

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'Benchmarks'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.10.

Benchmark suite Current: 961d992 Previous: cd08cf8 Ratio
Benchmarks.AttributedBenchmarks.LogWithName 394.08347562154137 ns (± 1.5931784498772503) 235.15561067263286 ns (± 0.9107873147635377) 1.68

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.