From 791790c98f3d28d37eef7509d43897d7d9dc17cc Mon Sep 17 00:00:00 2001 From: Ivan Maximov Date: Thu, 12 Dec 2024 20:05:17 +0300 Subject: [PATCH 1/2] Support string enumerables in LogReplacedAttribute --- README.md | 2 ++ .../ReplacedAttributeTests.cs | 26 +++++++++++++++++++ .../Attributed/LogReplacedAttribute.cs | 8 ++++-- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e655439..51d81f6 100644 --- a/README.md +++ b/README.md @@ -315,6 +315,8 @@ __Available properties__: - **Options:** The [RegexOptions](https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regexoptions?view=netcore-3.1) that will be applied. Defaults to __RegexOptions.None__. - **Timeout:** A time-out interval to evaluate regular expression. Defaults to __Regex.InfiniteMatchTimeout__. +Note that replacement also works for properties of type `IEnumerable` or derived from it, for example, `string[]` or `List`. + ### Examples diff --git a/src/Destructurama.Attributed.Tests/ReplacedAttributeTests.cs b/src/Destructurama.Attributed.Tests/ReplacedAttributeTests.cs index b2d2f47..c71bc58 100644 --- a/src/Destructurama.Attributed.Tests/ReplacedAttributeTests.cs +++ b/src/Destructurama.Attributed.Tests/ReplacedAttributeTests.cs @@ -38,6 +38,12 @@ public class CustomizedRegexLogs /// [LogReplaced("does not matter", "does not matter")] public int RegexReplaceForInt { get; set; } + + /// + /// 123|456|789 results in "***|456|****" + /// + [LogReplaced(REGEX_WITH_VERTICAL_BARS, "***|$2|****")] + public List? RegexForCollection { get; set; } } [TestFixture] @@ -156,4 +162,24 @@ public void LogReplacedAttribute_Should_Work_Only_For_String_Properties() props.ContainsKey("RegexReplaceForInt").ShouldBeFalse(); } + [Test] + public void LogReplacedAttribute_Should_Work_For_Collection_Of_String_Properties() + { + var customized = new CustomizedRegexLogs + { + RegexForCollection = ["123|456|789", "abc|def|ghi"] + }; + + var evt = DelegatingSink.Execute(customized); + + var sv = (StructureValue)evt.Properties["Customized"]; + var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); + + props.ContainsKey("RegexForCollection").ShouldBeTrue(); + var seq = props["RegexForCollection"].ShouldBeOfType(); + seq.Elements.Count.ShouldBe(2); + seq.Elements[0].LiteralValue().ShouldBe("***|456|****"); + seq.Elements[1].LiteralValue().ShouldBe("***|def|****"); + } + } diff --git a/src/Destructurama.Attributed/Attributed/LogReplacedAttribute.cs b/src/Destructurama.Attributed/Attributed/LogReplacedAttribute.cs index 30c4e18..f0ece3e 100644 --- a/src/Destructurama.Attributed/Attributed/LogReplacedAttribute.cs +++ b/src/Destructurama.Attributed/Attributed/LogReplacedAttribute.cs @@ -60,9 +60,13 @@ public bool TryCreateLogEventProperty(string name, object? value, ILogEventPrope if (value is string s) { - var replacement = Regex.Replace(s, _pattern, _replacement, Options, Timeout); + property = new(name, new ScalarValue(Regex.Replace(s, _pattern, _replacement, Options, Timeout))); + return true; + } - property = new(name, new ScalarValue(replacement)); + if (value is IEnumerable collection) + { + property = new(name, new SequenceValue(collection.Select(s => new ScalarValue(Regex.Replace(s, _pattern, _replacement, Options, Timeout))))); return true; } From cd1fa73960d8b2bedc479a50cb1489d29fdd7e18 Mon Sep 17 00:00:00 2001 From: Ivan Maximov Date: Sun, 15 Dec 2024 11:50:09 +0300 Subject: [PATCH 2/2] 1 --- src/Destructurama.Attributed.Tests/ReplacedAttributeTests.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Destructurama.Attributed.Tests/ReplacedAttributeTests.cs b/src/Destructurama.Attributed.Tests/ReplacedAttributeTests.cs index c71bc58..572be95 100644 --- a/src/Destructurama.Attributed.Tests/ReplacedAttributeTests.cs +++ b/src/Destructurama.Attributed.Tests/ReplacedAttributeTests.cs @@ -162,6 +162,7 @@ public void LogReplacedAttribute_Should_Work_Only_For_String_Properties() props.ContainsKey("RegexReplaceForInt").ShouldBeFalse(); } + [Test] public void LogReplacedAttribute_Should_Work_For_Collection_Of_String_Properties() {