Skip to content

Commit 1514169

Browse files
Add support for dictionaries in destructured objects
1 parent 6a6e6cc commit 1514169

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

Changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog for Serilog.Enrichers.Sensitive
22

3+
## 1.5.1
4+
5+
- Add support for dictionaries in destructured objects
6+
37
## 1.5.0
48

59
- Add support for collections in destructured objects

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<PropertyGroup>
3-
<Version>1.5.0.0</Version>
3+
<Version>1.5.1.0</Version>
44
<Authors>Sander van Vliet, Huibert Jan Nieuwkamer, Scott Toberman</Authors>
55
<Company>Codenizer BV</Company>
66
<Copyright>2022 Sander van Vliet</Copyright>

src/Serilog.Enrichers.Sensitive/SensitiveDataEnricher.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,28 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
161161

162162
return (anyMasked, new StructureValue(propList));
163163
}
164+
case DictionaryValue dictionaryValue:
165+
{
166+
var resultDictionary = new List<KeyValuePair<ScalarValue, LogEventPropertyValue>>();
167+
var anyKeyMasked = false;
168+
169+
foreach (var pair in dictionaryValue.Elements)
170+
{
171+
var (wasPairMasked, pairResult) = MaskProperty(new KeyValuePair<string, LogEventPropertyValue>(pair.Key.Value as string, pair.Value));
172+
173+
if (wasPairMasked)
174+
{
175+
resultDictionary.Add(new KeyValuePair<ScalarValue, LogEventPropertyValue>(pair.Key, pairResult));
176+
anyKeyMasked = true;
177+
}
178+
else
179+
{
180+
resultDictionary.Add(new KeyValuePair<ScalarValue, LogEventPropertyValue>(pair.Key, pair.Value));
181+
}
182+
}
183+
184+
return (anyKeyMasked, new DictionaryValue(resultDictionary));
185+
}
164186
default:
165187
return (false, null);
166188
}

test/Serilog.Enrichers.Sensitive.Tests.Unit/WhenMaskingDestructuredObject.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,38 @@ public void GivenDestructuredObjectIsCollectionOfObjects()
221221
sensitiveProperty.Value.ToString().Should().Be("\"***MASKED***\"");
222222
}
223223
}
224+
225+
[Fact]
226+
public void GivenDestructuredObjectIsADictionary()
227+
{
228+
var dictionary = new Dictionary<string, string>
229+
{
230+
{ "SensitiveProperty", "sensitive value"},
231+
{ "OtherProp", "not sensitive" }
232+
};
233+
234+
_logger.Information("Test message {@Dictionary}", dictionary);
235+
236+
var dictionaryProperty = _sink
237+
.Should()
238+
.HaveMessage("Test message {@Dictionary}")
239+
.Appearing()
240+
.Once()
241+
.WithProperty("Dictionary")
242+
.Subject as DictionaryValue;
243+
244+
dictionaryProperty
245+
.Elements[new ScalarValue("SensitiveProperty")]
246+
.ToString()
247+
.Should()
248+
.Be("\"***MASKED***\"");
249+
250+
dictionaryProperty
251+
.Elements[new ScalarValue("OtherProp")]
252+
.ToString()
253+
.Should()
254+
.Be("\"not sensitive\"");
255+
}
224256
}
225257

226258
public class TestObject

0 commit comments

Comments
 (0)