Skip to content

Commit 0b3b739

Browse files
committed
Add support for Enrich.With()
Support for `Enrich.With(ILogEventEnricher enricher)`
1 parent ed9c049 commit 0b3b739

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

src/Serilog.Settings.Configuration/Settings/Configuration/SurrogateConfigurationMethods.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public static IEnumerable<MethodInfo> Enrich
6060
get
6161
{
6262
yield return GetSurrogateConfigurationMethod<LoggerEnrichmentConfiguration, object, object>((c, _, __) => FromLogContext(c));
63+
yield return GetSurrogateConfigurationMethod<LoggerEnrichmentConfiguration, ILogEventEnricher, object>((c, e, __) => With(c, e));
6364
}
6465
}
6566

@@ -132,5 +133,10 @@ static LoggerConfiguration AsScalar(LoggerDestructuringConfiguration loggerDestr
132133
static LoggerConfiguration FromLogContext(LoggerEnrichmentConfiguration loggerEnrichmentConfiguration)
133134
=> loggerEnrichmentConfiguration.FromLogContext();
134135

136+
static LoggerConfiguration With(LoggerEnrichmentConfiguration loggerEnrichmentConfiguration, ILogEventEnricher enricher)
137+
{
138+
return loggerEnrichmentConfiguration.With(enricher);
139+
}
140+
135141
}
136142
}

test/Serilog.Settings.Configuration.Tests/ConfigurationSettingsTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,5 +962,34 @@ public void AuditToSinkIsAppliedWithCustomSinkAndLevelSwitch()
962962

963963
Assert.Single(DummyRollingFileSink.Emitted);
964964
}
965+
966+
967+
[Fact]
968+
public void EnrichWithIsAppliedWithCustomEnricher()
969+
{
970+
LogEvent evt = null;
971+
972+
var json = $@"{{
973+
""Serilog"": {{
974+
""Using"": [""TestDummies""],
975+
""Enrich"": [
976+
{{
977+
""Name"": ""With"",
978+
""Args"": {{
979+
""enricher"": ""{typeof(DummyThreadIdEnricher).AssemblyQualifiedName}""
980+
}}
981+
}}]
982+
}}
983+
}}";
984+
985+
var log = ConfigFromJson(json)
986+
.WriteTo.Sink(new DelegatingSink(e => evt = e))
987+
.CreateLogger();
988+
989+
log.Write(Some.InformationEvent());
990+
991+
Assert.NotNull(evt);
992+
Assert.True(evt.Properties.ContainsKey("ThreadId"), "Event should have enriched property ThreadId");
993+
}
965994
}
966995
}

test/TestDummies/DummyThreadIdEnricher.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ namespace TestDummies
66
public class DummyThreadIdEnricher : ILogEventEnricher
77
{
88
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
9-
{
9+
{
10+
logEvent.AddPropertyIfAbsent(propertyFactory
11+
.CreateProperty("ThreadId", "SomeId"));
1012
}
1113
}
1214
}

0 commit comments

Comments
 (0)