Skip to content

Commit daaf82d

Browse files
Enable configuration of the enricher through JSON
1 parent 4902c08 commit daaf82d

File tree

10 files changed

+123
-3
lines changed

10 files changed

+123
-3
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.7.0
4+
5+
- Allow configuration through `appsettings.json` files.
6+
37
## 1.6.0
48

59
- Pass match to `PreprocessMask` to allow for further customisation of mask value

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.6.0.0</Version>
3+
<Version>1.7.0.0</Version>
44
<Authors>Sander van Vliet, Huibert Jan Nieuwkamer, Scott Toberman</Authors>
55
<Company>Codenizer BV</Company>
66
<Copyright>2023 Sander van Vliet</Copyright>

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,33 @@ var logger = new LoggerConfiguration()
238238
.WriteTo.Console()
239239
.CreateLogger();
240240
```
241+
242+
## JSON configuration
243+
244+
If you are configuring your logger through `appsettings.json`, you can configure the enricher too. You will have to add a `Using` section if it doesn't exist already and include the `Serilog.Enrichers.Sensitive` assembly name there, otherwise configuration will silently fail.
245+
246+
```json
247+
{
248+
"Serilog": {
249+
"Using": [
250+
"Serilog.Enrichers.Sensitive"
251+
],
252+
"Enrich": [
253+
{
254+
"Name": "WithSensitiveDataMasking",
255+
"Args": {
256+
"options": {
257+
"MaskValue": "CUSTOM_MASK_FROM_JSON",
258+
"ExcludeProperties": [
259+
"email"
260+
],
261+
"Mode": "Globally"
262+
}
263+
}
264+
}
265+
]
266+
}
267+
}
268+
```
269+
270+
Note that `options` is the argument name of the `WithSensitiveDataMasking` extension method and must match exactly.

src/Serilog.Enrichers.Sensitive.Demo/Program.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Threading;
32
using System.Threading.Tasks;
43
using Serilog.Core;
54

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using System.Runtime.CompilerServices;
2+
3+
[assembly: InternalsVisibleTo("Serilog.Enrichers.Sensitive.Tests.Unit")]

src/Serilog.Enrichers.Sensitive/ExtensionMethods.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,15 @@ public static LoggerConfiguration WithSensitiveDataMasking(
7474

7575
public static LoggerConfiguration WithSensitiveDataMasking(
7676
this LoggerEnrichmentConfiguration loggerConfiguration,
77-
Action<SensitiveDataEnricherOptions> options)
77+
Action<SensitiveDataEnricherOptions> configure)
78+
{
79+
return loggerConfiguration
80+
.With(new SensitiveDataEnricher(configure));
81+
}
82+
83+
public static LoggerConfiguration WithSensitiveDataMasking(
84+
this LoggerEnrichmentConfiguration loggerConfiguration,
85+
SensitiveDataEnricherOptions options)
7886
{
7987
return loggerConfiguration
8088
.With(new SensitiveDataEnricher(options));

src/Serilog.Enrichers.Sensitive/SensitiveDataEnricher.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ internal class SensitiveDataEnricher : ILogEventEnricher
2020
private readonly List<string> _maskProperties;
2121
private readonly List<string> _excludeProperties;
2222

23+
public SensitiveDataEnricher(SensitiveDataEnricherOptions options)
24+
: this(options.Apply)
25+
{
26+
}
27+
2328
public SensitiveDataEnricher(
2429
Action<SensitiveDataEnricherOptions>? options)
2530
{

src/Serilog.Enrichers.Sensitive/SensitiveDataEnricherOptions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,18 @@ public class SensitiveDataEnricherOptions
3131
/// <para>This property takes precedence over <see cref="MaskProperties"/> and the masking operators.</para>
3232
/// </remarks>
3333
public List<string> ExcludeProperties { get; set; } = new List<string>();
34+
35+
/// <summary>
36+
/// Applies the settings of this <c>SensitiveDataEnricherOptions</c> instance to another <c>SensitiveDataEnricherOptions</c> instance
37+
/// </summary>
38+
/// <param name="other">The <c>SensitiveDataEnricherOptions</c> to apply the options to</param>
39+
public void Apply(SensitiveDataEnricherOptions other)
40+
{
41+
other.Mode = Mode;
42+
other.MaskValue = MaskValue;
43+
other.MaskingOperators = MaskingOperators;
44+
other.MaskProperties = MaskProperties;
45+
other.ExcludeProperties = ExcludeProperties;
46+
}
3447
}
3548
}

test/Serilog.Enrichers.Sensitive.Tests.Unit/Serilog.Enrichers.Sensitive.Tests.Unit.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@
1111

1212
<ItemGroup>
1313
<PackageReference Include="FluentAssertions" Version="6.7.0" />
14+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
15+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
1416
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
1517
<PackageReference Include="Serilog" Version="2.12.0" />
18+
<PackageReference Include="Serilog.Settings.Configuration" Version="3.4.0" />
1619
<PackageReference Include="Serilog.Sinks.InMemory" Version="0.11.0" />
1720
<PackageReference Include="Serilog.Sinks.InMemory.Assertions" Version="0.11.0" />
21+
<PackageReference Include="SerilogAnalyzer" Version="0.15.0" />
1822
<PackageReference Include="xunit" Version="2.4.2" />
1923
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
2024
<PrivateAssets>all</PrivateAssets>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System.IO;
2+
using System.Text;
3+
using Microsoft.Extensions.Configuration;
4+
using Serilog.Sinks.InMemory;
5+
using Serilog.Sinks.InMemory.Assertions;
6+
using Xunit;
7+
8+
namespace Serilog.Enrichers.Sensitive.Tests.Unit;
9+
10+
public class WhenConfiguringFromJson
11+
{
12+
[Fact]
13+
public void GivenJsonConfigurationWithMaskValue_EnricherIsConfiguredWithTheCorrectMaskValue()
14+
{
15+
var jsonConfiguration = @"
16+
{
17+
""Serilog"": {
18+
""Using"": [ ""Serilog.Enrichers.Sensitive"" ],
19+
""Enrich"": [ {
20+
""Name"": ""WithSensitiveDataMasking"",
21+
""Args"": {
22+
""options"": {
23+
""MaskValue"": ""^^"",
24+
""ExcludeProperties"": [ ""email"" ],
25+
""Mode"": ""Globally""
26+
}
27+
}
28+
}]
29+
}
30+
}
31+
";
32+
var memoryStream = new MemoryStream();
33+
memoryStream.Write(Encoding.UTF8.GetBytes(jsonConfiguration));
34+
memoryStream.Seek(0, SeekOrigin.Begin);
35+
36+
var configuration = new ConfigurationBuilder()
37+
.AddJsonStream(memoryStream)
38+
.Build();
39+
40+
var inMemorySink = new InMemorySink();
41+
42+
var logger = new LoggerConfiguration()
43+
.ReadFrom.Configuration(configuration)
44+
.WriteTo.Sink(inMemorySink)
45+
.CreateLogger();
46+
47+
logger.Information("Test value [email protected]");
48+
49+
inMemorySink
50+
.Should()
51+
.HaveMessage("Test value ^^", "the e-mail address is replaced with the configured masking value")
52+
.Appearing().Once();
53+
}
54+
}

0 commit comments

Comments
 (0)