Skip to content

Commit

Permalink
Fix LogWithNameAttribute on complex objects (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
sungam3r committed Feb 27, 2024
1 parent cd08cf8 commit 6de06e7
Show file tree
Hide file tree
Showing 4 changed files with 37 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
35 changes: 35 additions & 0 deletions src/Destructurama.Attributed.Tests/LogWithNameAttributeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,41 @@ public void AttributesAreConsultedWhenDestructuring(string name)
literalValue.ShouldBe(name);
}

// https://github.com/destructurama/attributed/issues/65
[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 6de06e7

@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: 6de06e7 Previous: cd08cf8 Ratio
Benchmarks.AttributedBenchmarks.LogWithName 404.87072839736936 ns (± 1.64457468464245) 235.15561067263286 ns (± 0.9107873147635377) 1.72

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

Please sign in to comment.