Skip to content

Commit 0ada1b1

Browse files
authored
Support string enumerables in LogReplacedAttribute (#127)
1 parent 9c7ee1d commit 0ada1b1

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,8 @@ __Available properties__:
345345
- **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__.
346346
- **Timeout:** A time-out interval to evaluate regular expression. Defaults to __Regex.InfiniteMatchTimeout__.
347347

348+
Note that replacement also works for properties of type `IEnumerable<string>` or derived from it, for example, `string[]` or `List<string>`.
349+
348350
### Examples
349351

350352
<!-- snippet: WithRegex -->

src/Destructurama.Attributed.Tests/ReplacedAttributeTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ public class CustomizedRegexLogs
3838
/// </summary>
3939
[LogReplaced("does not matter", "does not matter")]
4040
public int RegexReplaceForInt { get; set; }
41+
42+
/// <summary>
43+
/// 123|456|789 results in "***|456|****"
44+
/// </summary>
45+
[LogReplaced(REGEX_WITH_VERTICAL_BARS, "***|$2|****")]
46+
public List<string>? RegexForCollection { get; set; }
4147
}
4248

4349
[TestFixture]
@@ -156,4 +162,25 @@ public void LogReplacedAttribute_Should_Work_Only_For_String_Properties()
156162

157163
props.ContainsKey("RegexReplaceForInt").ShouldBeFalse();
158164
}
165+
166+
[Test]
167+
public void LogReplacedAttribute_Should_Work_For_Collection_Of_String_Properties()
168+
{
169+
var customized = new CustomizedRegexLogs
170+
{
171+
RegexForCollection = ["123|456|789", "abc|def|ghi"]
172+
};
173+
174+
var evt = DelegatingSink.Execute(customized);
175+
176+
var sv = (StructureValue)evt.Properties["Customized"];
177+
var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value);
178+
179+
props.ContainsKey("RegexForCollection").ShouldBeTrue();
180+
var seq = props["RegexForCollection"].ShouldBeOfType<SequenceValue>();
181+
seq.Elements.Count.ShouldBe(2);
182+
seq.Elements[0].LiteralValue().ShouldBe("***|456|****");
183+
seq.Elements[1].LiteralValue().ShouldBe("***|def|****");
184+
}
185+
159186
}

src/Destructurama.Attributed/Attributed/LogReplacedAttribute.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,13 @@ public bool TryCreateLogEventProperty(string name, object? value, ILogEventPrope
6060

6161
if (value is string s)
6262
{
63-
var replacement = Regex.Replace(s, _pattern, _replacement, Options, Timeout);
63+
property = new(name, new ScalarValue(Regex.Replace(s, _pattern, _replacement, Options, Timeout)));
64+
return true;
65+
}
6466

65-
property = new(name, new ScalarValue(replacement));
67+
if (value is IEnumerable<string> collection)
68+
{
69+
property = new(name, new SequenceValue(collection.Select(s => new ScalarValue(Regex.Replace(s, _pattern, _replacement, Options, Timeout)))));
6670
return true;
6771
}
6872

0 commit comments

Comments
 (0)