diff --git a/src/Destructurama.Attributed.Tests/MetadataTypeTests.cs b/src/Destructurama.Attributed.Tests/MetadataTypeTests.cs new file mode 100644 index 0000000..46fa84f --- /dev/null +++ b/src/Destructurama.Attributed.Tests/MetadataTypeTests.cs @@ -0,0 +1,43 @@ +using System.ComponentModel.DataAnnotations; +using Destructurama.Attributed.Tests.Support; +using NUnit.Framework; +using Serilog.Events; +using Shouldly; + +namespace Destructurama.Attributed.Tests; + +[TestFixture] +public class MetadataTypeTests +{ + [Test] + public void MetadataType_Should_Be_Respected() + { + var customized = new Dto + { + Private = "secret", + Public = "not_Secret" + }; + + var evt = DelegatingSink.Execute(customized); + + var sv = (StructureValue)evt.Properties["Customized"]; + var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); + + props.Count.ShouldBe(1); + props["Public"].LiteralValue().ShouldBe("not_Secret"); + } + + [MetadataType(typeof(DtoMetadata))] + public partial class Dto + { + public string Private { get; set; } + + public string Public { get; set; } + } + + internal class DtoMetadata + { + [NotLogged] + public object Private { get; set; } + } +} diff --git a/src/Destructurama.Attributed/Attributed/AttributedDestructuringPolicy.cs b/src/Destructurama.Attributed/Attributed/AttributedDestructuringPolicy.cs index 6dbebc6..d90c377 100644 --- a/src/Destructurama.Attributed/Attributed/AttributedDestructuringPolicy.cs +++ b/src/Destructurama.Attributed/Attributed/AttributedDestructuringPolicy.cs @@ -74,6 +74,8 @@ private CacheEntry CreateCacheEntry(Type type) if (classDestructurer != null) return new(classDestructurer.CreateLogEventPropertyValue); + // TODO: fetch ti.GetCustomAttribute(); and bind to properties + var properties = GetPropertiesRecursive(type).ToList(); if (!_options.IgnoreNullProperties && properties.All(pi => GetCustomAttribute(pi) == null diff --git a/src/Destructurama.Attributed/Destructurama.Attributed.csproj b/src/Destructurama.Attributed/Destructurama.Attributed.csproj index d7ca12d..43d2bdb 100644 --- a/src/Destructurama.Attributed/Destructurama.Attributed.csproj +++ b/src/Destructurama.Attributed/Destructurama.Attributed.csproj @@ -1,7 +1,7 @@ - netstandard2.0 + netstandard2.0;netstandard2.1 Use attributes to control how complex types are logged to Serilog. Destructurama true @@ -10,6 +10,7 @@ +