A Serilog enricher automatically adds contextual properties to log events, avoiding repeated value retrieval and keeping those values out of message text. Common properties include the machine name, thread ID, process ID, and environment name. section 7.4.1 section 7.4.5
Configure an enricher in the board-game API logging setup by:
- Installing the dedicated Serilog.Enrichers.Environment and Serilog.Enrichers.Thread NuGet packages. section 7.4.5
- Activating the features through the Serilog logger configuration with WithMachineName() and WithThreadId(). section 7.4.5
- Choosing whether enriched values remain in the semistructured Properties data or receive dedicated sink columns. section 7.4.5
- Adding properties such as MachineName and ThreadId to a file sink output template when readable text logs need them. section 7.4.5
builder.Host.UseSerilog((ctx, lc) => {
lc.ReadFrom.Configuration(ctx.Configuration);
lc.Enrich.WithMachineName(); #1
lc.Enrich.WithThreadId(); #2
The configuration listing shows the two enrichment calls placed inside UseSerilog, alongside configuration loading. section 7.4.5
Adding a thread name
Extend the current log configuration with a ThreadName property, then modify the file sink template so ThreadName appears immediately after ThreadId. This demonstrates that enrichment and output formatting are separate configuration decisions. section 7.5.4
Verify the result by inspecting generated log records or the log file: enriched values should appear automatically on each applicable event, while the message remains focused on the application event. section 7.4.5
