From 82a50b5f5929a17f06b8d0ee88f7ca220e640c9f Mon Sep 17 00:00:00 2001 From: andreas Date: Mon, 9 Dec 2024 13:42:03 +0200 Subject: [PATCH 1/4] LogMaskedAttribute to handle int and long values --- README.md | 28 ++++- .../MaskedAttributeTests.cs | 102 ++++++++++++++++++ .../Attributed/LogMaskedAttribute.cs | 4 +- 3 files changed, 131 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f894ca2..ba16744 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ public class PersonalData public string? Name { get; set; } } ``` -snippet source | anchor +snippet source | anchor ## 2. Ignoring a property @@ -169,6 +169,18 @@ public class CustomizedMaskedLogs [LogMasked] public string? DefaultMasked { get; set; } + /// + /// 9223372036854775807 results in "***" + /// + [LogMasked] + public long? DefaultMaskedLong { get; set; } + + /// + /// 2147483647 results in "***" + /// + [LogMasked] + public int? DefaultMaskedInt { get; set; } + /// /// [123456789,123456789,123456789] results in [***,***,***] /// @@ -211,6 +223,18 @@ public class CustomizedMaskedLogs [LogMasked(ShowFirst = 3)] public string? ShowFirstThreeThenDefaultMasked { get; set; } + /// + /// 9223372036854775807 results in "922***807" + /// + [LogMasked(ShowFirst = 3, ShowLast = 3)] + public long? ShowFirstAndLastThreeAndDefaultMaskLongInTheMiddle { get; set; } + + /// + /// 2147483647 results in "214****647" + /// + [LogMasked(ShowFirst = 3, ShowLast = 3, PreserveLength = true)] + public long? ShowFirstAndLastThreeAndDefaultMaskIntInTheMiddlePreservedLength { get; set; } + /// /// 123456789 results in "123******" /// @@ -284,7 +308,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 fc53f6f..07a2063 100644 --- a/src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs +++ b/src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs @@ -15,6 +15,18 @@ public class CustomizedMaskedLogs [LogMasked] public string? DefaultMasked { get; set; } + /// + /// 9223372036854775807 results in "***" + /// + [LogMasked] + public long? DefaultMaskedLong { get; set; } + + /// + /// 2147483647 results in "***" + /// + [LogMasked] + public int? DefaultMaskedInt { get; set; } + /// /// [123456789,123456789,123456789] results in [***,***,***] /// @@ -57,6 +69,18 @@ public class CustomizedMaskedLogs [LogMasked(ShowFirst = 3)] public string? ShowFirstThreeThenDefaultMasked { get; set; } + /// + /// 9223372036854775807 results in "922***807" + /// + [LogMasked(ShowFirst = 3, ShowLast = 3)] + public long? ShowFirstAndLastThreeAndDefaultMaskLongInTheMiddle { get; set; } + + /// + /// 2147483647 results in "214****647" + /// + [LogMasked(ShowFirst = 3, ShowLast = 3, PreserveLength = true)] + public long? ShowFirstAndLastThreeAndDefaultMaskIntInTheMiddlePreservedLength { get; set; } + /// /// 123456789 results in "123******" /// @@ -690,4 +714,82 @@ public void LogMaskedAttribute_Shows_First_NChars_And_Last_NChars_Then_Replaces_ props.ContainsKey("ShowFirstAndLastThreeAndCustomMaskInTheMiddlePreservedLengthIgnored").ShouldBeTrue(); props["ShowFirstAndLastThreeAndCustomMaskInTheMiddlePreservedLengthIgnored"].LiteralValue().ShouldBe("123_REMOVED_321"); } + + [Test] + public void LogMaskedAttribute_Replaces_Long_Value_With_DefaultStars_Mask() + { + // [LogMasked] + // 9223372036854775807 -> "***" + var customized = new CustomizedMaskedLogs + { + DefaultMaskedLong = long.MaxValue + }; + + var evt = DelegatingSink.Execute(customized); + + var sv = (StructureValue)evt.Properties["Customized"]; + var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); + + props.ContainsKey("DefaultMaskedLong").ShouldBeTrue(); + props["DefaultMaskedLong"].LiteralValue().ShouldBe("***"); + } + + [Test] + public void LogMaskedAttribute_Shows_First_NChars_And_Last_NChars_Replaces_Long_Value_With_Default_StarMask() + { + // [LogMasked(ShowFirst = 3, ShowLast = 3)] + // 9223372036854775807 -> "922***807" + var customized = new CustomizedMaskedLogs + { + ShowFirstAndLastThreeAndDefaultMaskLongInTheMiddle = long.MaxValue + }; + + var evt = DelegatingSink.Execute(customized); + + var sv = (StructureValue)evt.Properties["Customized"]; + var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); + + props.ContainsKey("ShowFirstAndLastThreeAndDefaultMaskLongInTheMiddle").ShouldBeTrue(); + props["ShowFirstAndLastThreeAndDefaultMaskLongInTheMiddle"].LiteralValue().ShouldBe("922***807"); + } + + [Test] + public void LogMaskedAttribute_Replaces_Int_Value_With_DefaultStars_Mask() + { + // [LogMasked] + // 2147483647 -> "***" + var customized = new CustomizedMaskedLogs + { + DefaultMaskedInt = int.MaxValue + }; + + var evt = DelegatingSink.Execute(customized); + + var sv = (StructureValue)evt.Properties["Customized"]; + var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); + + props.ContainsKey("DefaultMaskedInt").ShouldBeTrue(); + props["DefaultMaskedInt"].LiteralValue().ShouldBe("***"); + } + + [Test] + public void LogMaskedAttribute_Shows_First_NChars_And_Last_NChars_Replaces_Int_Value_With_Default_StarMask_And_PreservedLength() + { + // [LogMasked(ShowFirst = 3, ShowLast = 3, PreserveLength = true)] + // 2147483647 -> "214****647" + + var customized = new CustomizedMaskedLogs + { + ShowFirstAndLastThreeAndDefaultMaskIntInTheMiddlePreservedLength = int.MaxValue + }; + + var evt = DelegatingSink.Execute(customized); + + var sv = (StructureValue)evt.Properties["Customized"]; + var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); + + props.ContainsKey("ShowFirstAndLastThreeAndDefaultMaskIntInTheMiddlePreservedLength").ShouldBeTrue(); + props["ShowFirstAndLastThreeAndDefaultMaskIntInTheMiddlePreservedLength"].LiteralValue().ShouldBe("214****647"); + } + } diff --git a/src/Destructurama.Attributed/Attributed/LogMaskedAttribute.cs b/src/Destructurama.Attributed/Attributed/LogMaskedAttribute.cs index 4f61694..f2ce9b3 100644 --- a/src/Destructurama.Attributed/Attributed/LogMaskedAttribute.cs +++ b/src/Destructurama.Attributed/Attributed/LogMaskedAttribute.cs @@ -119,7 +119,9 @@ private LogEventPropertyValue CreateValue(object? value) { IEnumerable strings => new SequenceValue(strings.Select(s => new ScalarValue(FormatMaskedValue(s)))), string s => new ScalarValue(FormatMaskedValue(s)), - _ => ScalarValue.Null + long l => new ScalarValue(FormatMaskedValue(l.ToString())), + int i => new ScalarValue(FormatMaskedValue(i.ToString())), + _ => ScalarValue.Null, }; } } From a35a45e6b55aa88c58fc4544ce7b91c14c667764 Mon Sep 17 00:00:00 2001 From: andreas Date: Fri, 13 Dec 2024 09:22:40 +0200 Subject: [PATCH 2/4] Add support for Guid https://github.com/destructurama/attributed/pull/122/files --- README.md | 28 +++++++++++-------- .../MaskedAttributeTests.cs | 27 +++++++++++++++++- .../Attributed/LogMaskedAttribute.cs | 1 + 3 files changed, 44 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index ba16744..f45829b 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ var log = new LoggerConfiguration() Apply the `LogWithName` attribute: - + ```cs public class PersonalData { @@ -55,7 +55,7 @@ public class PersonalData public string? Name { get; set; } } ``` -snippet source | anchor +snippet source | anchor ## 2. Ignoring a property @@ -63,7 +63,7 @@ public class PersonalData Apply the `NotLogged` attribute: - + ```cs public class LoginCommand { @@ -73,18 +73,18 @@ public class LoginCommand public string? Password { get; set; } } ``` -snippet source | anchor +snippet source | anchor When the object is passed using `{@...}` syntax the attributes will be consulted. - + ```cs var command = new LoginCommand { Username = "logged", Password = "not logged" }; _log.Information("Logging in {@Command}", command); ``` -snippet source | anchor +snippet source | anchor ## 3. Ignoring a property if it has default value @@ -159,7 +159,7 @@ Note that masking also works for properties of type `IEnumerable` or der ### Examples - + ```cs public class CustomizedMaskedLogs { @@ -254,11 +254,17 @@ public class CustomizedMaskedLogs public string? ShowLastThreeThenDefaultMaskedPreservedLength { get; set; } /// - /// 123456789 results in "123REMOVED" + /// 123456789 results in "123_REMOVED_" /// [LogMasked(Text = "_REMOVED_", ShowFirst = 3)] public string? ShowFirstThreeThenCustomMask { get; set; } + /// + /// d3c4a1f2-3b4e-4f5a-9b6c-7d8e9f0a1b2c results in "d3c4a_REMOVED_" + /// + [LogMasked(Text = "_REMOVED_", ShowFirst = 5)] + public Guid? ShowFirstFiveThenCustomMaskGuid { get; set; } + /// /// 123456789 results in "123_REMOVED_" /// @@ -308,7 +314,7 @@ public class CustomizedMaskedLogs public string? ShowFirstAndLastThreeAndCustomMaskInTheMiddlePreservedLengthIgnored { get; set; } } ``` -snippet source | anchor +snippet source | anchor ## 7. Masking a string property with regular expressions @@ -342,7 +348,7 @@ __Available properties__: ### Examples - + ```cs public class WithRegex { @@ -361,7 +367,7 @@ public class WithRegex public string? RegexReplaceSecond { get; set; } } ``` -snippet source | anchor +snippet source | anchor # Benchmarks diff --git a/src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs b/src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs index 07a2063..97388bd 100644 --- a/src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs +++ b/src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs @@ -100,11 +100,17 @@ public class CustomizedMaskedLogs public string? ShowLastThreeThenDefaultMaskedPreservedLength { get; set; } /// - /// 123456789 results in "123REMOVED" + /// 123456789 results in "123_REMOVED_" /// [LogMasked(Text = "_REMOVED_", ShowFirst = 3)] public string? ShowFirstThreeThenCustomMask { get; set; } + /// + /// d3c4a1f2-3b4e-4f5a-9b6c-7d8e9f0a1b2c results in "d3c4a_REMOVED_" + /// + [LogMasked(Text = "_REMOVED_", ShowFirst = 5)] + public Guid? ShowFirstFiveThenCustomMaskGuid { get; set; } + /// /// 123456789 results in "123_REMOVED_" /// @@ -313,6 +319,25 @@ public void LogMaskedAttribute_Shows_First_NChars_Then_Replaces_All_With_Custom_ props["ShowFirstThreeThenCustomMask"].LiteralValue().ShouldBe("123_REMOVED_"); } + [Test] + public void LogMaskedAttribute_Shows_First_NChars_Then_Replaces_All_With_Custom_Mask_Guid() + { + // [LogMasked(Text = "_REMOVED_", ShowFirst = 5)] + // -> "d3c4a_REMOVED_" + var customized = new CustomizedMaskedLogs + { + ShowFirstFiveThenCustomMaskGuid = Guid.Parse("d3c4a1f2-3b4e-4f5a-9b6c-7d8e9f0a1b2c") + }; + + var evt = DelegatingSink.Execute(customized); + + var sv = (StructureValue)evt.Properties["Customized"]; + var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); + + props.ContainsKey("ShowFirstFiveThenCustomMaskGuid").ShouldBeTrue(); + props["ShowFirstFiveThenCustomMaskGuid"].LiteralValue().ShouldBe("d3c4a_REMOVED_"); + } + [Test] public void LogMaskedAttribute_Shows_First_NChars_Then_Replaces_All_With_Custom_Mask_PreservedLength_Ignored() { diff --git a/src/Destructurama.Attributed/Attributed/LogMaskedAttribute.cs b/src/Destructurama.Attributed/Attributed/LogMaskedAttribute.cs index f2ce9b3..6caa70a 100644 --- a/src/Destructurama.Attributed/Attributed/LogMaskedAttribute.cs +++ b/src/Destructurama.Attributed/Attributed/LogMaskedAttribute.cs @@ -121,6 +121,7 @@ private LogEventPropertyValue CreateValue(object? value) 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, }; } From eafa91e3021310c4c4d63e7b706565b078fe4261 Mon Sep 17 00:00:00 2001 From: Ivan Maximov Date: Sun, 15 Dec 2024 11:40:55 +0300 Subject: [PATCH 3/4] Apply suggestions from code review --- README.md | 6 +++--- src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index b366f7c..d0474c7 100644 --- a/README.md +++ b/README.md @@ -233,7 +233,7 @@ public class CustomizedMaskedLogs /// 2147483647 results in "214****647" /// [LogMasked(ShowFirst = 3, ShowLast = 3, PreserveLength = true)] - public long? ShowFirstAndLastThreeAndDefaultMaskIntInTheMiddlePreservedLength { get; set; } + public int? ShowFirstAndLastThreeAndDefaultMaskIntInTheMiddlePreservedLength { get; set; } /// /// 123456789 results in "123******" @@ -254,7 +254,7 @@ public class CustomizedMaskedLogs public string? ShowLastThreeThenDefaultMaskedPreservedLength { get; set; } /// - /// 123456789 results in "123_REMOVED_" + /// 123456789 results in "123_REMOVED_" /// [LogMasked(Text = "_REMOVED_", ShowFirst = 3)] public string? ShowFirstThreeThenCustomMask { get; set; } @@ -266,7 +266,7 @@ public class CustomizedMaskedLogs public Guid? ShowFirstFiveThenCustomMaskGuid { get; set; } /// - /// 123456789 results in "123_REMOVED_" + /// 123456789 results in "123_REMOVED_" /// [LogMasked(Text = "_REMOVED_", ShowFirst = 3, PreserveLength = true)] public string? ShowFirstThreeThenCustomMaskPreservedLengthIgnored { get; set; } diff --git a/src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs b/src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs index ac86c86..3c769f3 100644 --- a/src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs +++ b/src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs @@ -79,7 +79,7 @@ public class CustomizedMaskedLogs /// 2147483647 results in "214****647" /// [LogMasked(ShowFirst = 3, ShowLast = 3, PreserveLength = true)] - public long? ShowFirstAndLastThreeAndDefaultMaskIntInTheMiddlePreservedLength { get; set; } + public int? ShowFirstAndLastThreeAndDefaultMaskIntInTheMiddlePreservedLength { get; set; } /// /// 123456789 results in "123******" @@ -100,7 +100,7 @@ public class CustomizedMaskedLogs public string? ShowLastThreeThenDefaultMaskedPreservedLength { get; set; } /// - /// 123456789 results in "123_REMOVED_" + /// 123456789 results in "123_REMOVED_" /// [LogMasked(Text = "_REMOVED_", ShowFirst = 3)] public string? ShowFirstThreeThenCustomMask { get; set; } From 3f95dbc33f126c34dd6cb80d3f037a93d970a31a Mon Sep 17 00:00:00 2001 From: Ivan Maximov Date: Sun, 15 Dec 2024 11:42:12 +0300 Subject: [PATCH 4/4] Update src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs --- src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs b/src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs index 3c769f3..80686ac 100644 --- a/src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs +++ b/src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs @@ -112,7 +112,7 @@ public class CustomizedMaskedLogs public Guid? ShowFirstFiveThenCustomMaskGuid { get; set; } /// - /// 123456789 results in "123_REMOVED_" + /// 123456789 results in "123_REMOVED_" /// [LogMasked(Text = "_REMOVED_", ShowFirst = 3, PreserveLength = true)] public string? ShowFirstThreeThenCustomMaskPreservedLengthIgnored { get; set; }