From 90a7d1e2773bad0dca0bd61a063742657ea0f417 Mon Sep 17 00:00:00 2001 From: Ivan Maximov Date: Wed, 18 Dec 2024 22:15:29 +0300 Subject: [PATCH] LogMasked works for properties of any type --- README.md | 11 +++++-- .../MaskedAttributeTests.cs | 30 +++++++++++++++++-- .../Attributed/LogMaskedAttribute.cs | 7 ++--- 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 85a49e0..d228a6e 100644 --- a/README.md +++ b/README.md @@ -145,7 +145,7 @@ var log = new LoggerConfiguration() To prevent destructuring of a type or property at all, apply the `LogAsScalar` attribute. -## 6. Masking a string property +## 6. Masking a property Apply the `LogMasked` attribute with various settings: @@ -154,6 +154,7 @@ Apply the `LogMasked` attribute with various settings: - **ShowLast:** Shows the last x characters in the property value. - **PreserveLength:** If set, it will swap out each character with the default value. Note that this property will be ignored if Text has been set to custom value. +Masking works for all properties calling `ToString()` on their values. Note that masking also works for properties of type `IEnumerable` or derived from it, for example, `string[]` or `List`. ### Examples @@ -265,6 +266,12 @@ public class CustomizedMaskedLogs [LogMasked(Text = "_REMOVED_", ShowFirst = 5)] public Guid? ShowFirstFiveThenCustomMaskGuid { get; set; } + /// + /// Descending results in "Desce_REMOVED_" + /// + [LogMasked(Text = "_REMOVED_", ShowFirst = 5)] + public ListSortDirection ShowFirstFiveThenCustomMaskEnum { get; set; } + /// /// 123456789 results in "123_REMOVED_" /// @@ -314,7 +321,7 @@ public class CustomizedMaskedLogs public string? ShowFirstAndLastThreeAndCustomMaskInTheMiddlePreservedLengthIgnored { get; set; } } ``` -snippet source | anchor +snippet source | anchor ## 7. Masking a string property with regular expressions diff --git a/src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs b/src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs index 80686ac..c7f5372 100644 --- a/src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs +++ b/src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs @@ -1,3 +1,4 @@ +using System.ComponentModel; using Destructurama.Attributed.Tests.Support; using NUnit.Framework; using Serilog.Events; @@ -111,6 +112,12 @@ public class CustomizedMaskedLogs [LogMasked(Text = "_REMOVED_", ShowFirst = 5)] public Guid? ShowFirstFiveThenCustomMaskGuid { get; set; } + /// + /// Descending results in "Desce_REMOVED_" + /// + [LogMasked(Text = "_REMOVED_", ShowFirst = 5)] + public ListSortDirection ShowFirstFiveThenCustomMaskEnum { get; set; } + /// /// 123456789 results in "123_REMOVED_" /// @@ -338,6 +345,25 @@ public void LogMaskedAttribute_Shows_First_NChars_Then_Replaces_All_With_Custom_ props["ShowFirstFiveThenCustomMaskGuid"].LiteralValue().ShouldBe("d3c4a_REMOVED_"); } + [Test] + public void LogMaskedAttribute_Shows_First_NChars_Then_Replaces_All_With_Custom_Mask_Enum() + { + // [LogMasked(Text = "_REMOVED_", ShowFirst = 5)] + // -> "Desce_REMOVED_" + var customized = new CustomizedMaskedLogs + { + ShowFirstFiveThenCustomMaskEnum = ListSortDirection.Descending, + }; + + var evt = DelegatingSink.Execute(customized); + + var sv = (StructureValue)evt.Properties["Customized"]; + var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); + + props.ContainsKey("ShowFirstFiveThenCustomMaskEnum").ShouldBeTrue(); + props["ShowFirstFiveThenCustomMaskEnum"].LiteralValue().ShouldBe("Desce_REMOVED_"); + } + [Test] public void LogMaskedAttribute_Shows_First_NChars_Then_Replaces_All_With_Custom_Mask_PreservedLength_Ignored() { @@ -819,7 +845,7 @@ public void LogMaskedAttribute_Shows_First_NChars_And_Last_NChars_Replaces_Int_V [Test] - public void LogMaskedAttribute_Nullify_Bool_Property() + public void LogMaskedAttribute_Mask_Bool_Property() { var customized = new CustomizedMaskedLogs2 { @@ -836,7 +862,7 @@ public void LogMaskedAttribute_Nullify_Bool_Property() props["Enabled1"].LiteralValue().ShouldBe(true); props.ContainsKey("Enabled2").ShouldBeTrue(); - props["Enabled2"].LiteralValue().ShouldBeNull(); + props["Enabled2"].LiteralValue().ShouldBe("***"); } private class CustomizedMaskedLogs2 diff --git a/src/Destructurama.Attributed/Attributed/LogMaskedAttribute.cs b/src/Destructurama.Attributed/Attributed/LogMaskedAttribute.cs index 6caa70a..a03e375 100644 --- a/src/Destructurama.Attributed/Attributed/LogMaskedAttribute.cs +++ b/src/Destructurama.Attributed/Attributed/LogMaskedAttribute.cs @@ -118,11 +118,8 @@ private LogEventPropertyValue CreateValue(object? value) return value switch { IEnumerable strings => new SequenceValue(strings.Select(s => new ScalarValue(FormatMaskedValue(s)))), - string s => new ScalarValue(FormatMaskedValue(s)), - long l => new ScalarValue(FormatMaskedValue(l.ToString())), - int i => new ScalarValue(FormatMaskedValue(i.ToString())), - Guid g => new ScalarValue(FormatMaskedValue(g.ToString())), - _ => ScalarValue.Null, + null => ScalarValue.Null, + _ => new ScalarValue(FormatMaskedValue(value.ToString())), }; } }