Skip to content

Commit

Permalink
Add support for dictionaries in destructured objects
Browse files Browse the repository at this point in the history
  • Loading branch information
sandermvanvliet committed Oct 20, 2022
1 parent 6a6e6cc commit 1514169
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog for Serilog.Enrichers.Sensitive

## 1.5.1

- Add support for dictionaries in destructured objects

## 1.5.0

- Add support for collections in destructured objects
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>1.5.0.0</Version>
<Version>1.5.1.0</Version>
<Authors>Sander van Vliet, Huibert Jan Nieuwkamer, Scott Toberman</Authors>
<Company>Codenizer BV</Company>
<Copyright>2022 Sander van Vliet</Copyright>
Expand Down
22 changes: 22 additions & 0 deletions src/Serilog.Enrichers.Sensitive/SensitiveDataEnricher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,28 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)

return (anyMasked, new StructureValue(propList));
}
case DictionaryValue dictionaryValue:
{
var resultDictionary = new List<KeyValuePair<ScalarValue, LogEventPropertyValue>>();
var anyKeyMasked = false;

foreach (var pair in dictionaryValue.Elements)
{
var (wasPairMasked, pairResult) = MaskProperty(new KeyValuePair<string, LogEventPropertyValue>(pair.Key.Value as string, pair.Value));

if (wasPairMasked)
{
resultDictionary.Add(new KeyValuePair<ScalarValue, LogEventPropertyValue>(pair.Key, pairResult));
anyKeyMasked = true;
}
else
{
resultDictionary.Add(new KeyValuePair<ScalarValue, LogEventPropertyValue>(pair.Key, pair.Value));
}
}

return (anyKeyMasked, new DictionaryValue(resultDictionary));
}
default:
return (false, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,38 @@ public void GivenDestructuredObjectIsCollectionOfObjects()
sensitiveProperty.Value.ToString().Should().Be("\"***MASKED***\"");
}
}

[Fact]
public void GivenDestructuredObjectIsADictionary()
{
var dictionary = new Dictionary<string, string>
{
{ "SensitiveProperty", "sensitive value"},
{ "OtherProp", "not sensitive" }
};

_logger.Information("Test message {@Dictionary}", dictionary);

var dictionaryProperty = _sink
.Should()
.HaveMessage("Test message {@Dictionary}")
.Appearing()
.Once()
.WithProperty("Dictionary")
.Subject as DictionaryValue;

dictionaryProperty
.Elements[new ScalarValue("SensitiveProperty")]
.ToString()
.Should()
.Be("\"***MASKED***\"");

dictionaryProperty
.Elements[new ScalarValue("OtherProp")]
.ToString()
.Should()
.Be("\"not sensitive\"");
}
}

public class TestObject
Expand Down

0 comments on commit 1514169

Please sign in to comment.