liveBook · Manning

Enricher

As explained in Building Web APIs with ASP.NET Core

An enricher is a Serilog extension component that automatically adds contextual properties to log events.

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

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Explains package installation, activation, storage choices, and output formatting, but does not implement an enricher internally.
Examples
Uses MachineName, ThreadId, ThreadName, Properties storage, and file output templates as concrete enrichment examples.
What you can do after
Apply it via Serilog configuration to add machine, thread, or custom context properties without repeating logging code.
How it connects
Connects enrichers with Serilog, message templates, structured logging, sinks, and ASP.NET Core logging.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.