Skip to content

Commit

Permalink
Don't capture properties with private get accessors
Browse files Browse the repository at this point in the history
  • Loading branch information
nblumhardt committed Aug 26, 2024
1 parent 3f6d855 commit 68881e1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Serilog/Capturing/PropertyValueConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ internal StructureValue CreateStructureValue(object value, [DynamicallyAccessedM
for (var i = 0; i < properties.Length; ++i)
{
var property = properties[i];
if (!property.CanRead)
if (property.GetMethod == null || !property.GetMethod.IsPublic)
{
continue;
}
Expand Down
14 changes: 14 additions & 0 deletions test/Serilog.Tests/Capturing/PropertyValueConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -488,4 +488,18 @@ public class SubclassWithOverride : BaseClass
{
public override string Name => "override";
}

[Fact]
public void PrivateGettersAreIgnoredWhenCapturing()
{
var structure = _converter.CreateStructureValue(new HasPropertyWithPrivateGetter(), typeof(HasPropertyWithPrivateGetter), false);
Assert.Contains(structure.Properties, p => p.Name == "A");
Assert.DoesNotContain(structure.Properties, p => p.Name == "B");
}

public class HasPropertyWithPrivateGetter
{
public string A { get; set; } = "A";
public string B { private get; set; } = "B";
}
}

0 comments on commit 68881e1

Please sign in to comment.