-
Notifications
You must be signed in to change notification settings - Fork 798
Enrichment
Log events can be enriched with properties in various ways. A number of pre-built enrichers are provided through NuGet:
Install-Package Serilog.Enrichers.Thread
Configuration for enrichment is done via the Enrich
configuration object:
var log = new LoggerConfiguration()
.Enrich.WithThreadId()
.WriteTo.Console()
.CreateLogger();
All events written through log
will carry a property ThreadId
with the id of the managed thread that wrote them. (By convention, any .WithXyz()
methods on Enrich
create properties named Xyz
.)
Serilog.Context.LogContext
can be used to dynamically add and remove properties from the ambient "execution context"; for example, all messages written during a transaction might carry the id of that transaction, and so-on.
This feature must be added to the logger at configuration-time using .FromLogContext()
:
var log = new LoggerConfiguration()
.Enrich.FromLogContext()
Then, properties can be added and removed from the context using LogContext.PushProperty()
:
log.Information("No contextual properties");
using (LogContext.PushProperty("A", 1))
{
log.Information("Carries property A = 1");
using (LogContext.PushProperty("A", 2))
using (LogContext.PushProperty("B", 1))
{
log.Information("Carries A = 2 and B = 1");
}
log.Information("Carries property A = 1, again");
}
Pushing property onto the context will override any existing properties with the same name, until the object returned from PushProperty()
is disposed, as the property A
in the example demonstrates.
Important: properties must be popped from the context in the precise order in which they were added. Behavior otherwise is undefined.
The Serilog project provides:
-
Serilog.Enrichers.Environment -
WithMachineName()
andWithEnvironmentUserName()
-
Serilog.Enrichers.Process -
WithProcessId()
-
Serilog.Enrichers.Thread -
WithThreadId()
Other interesting enrichers:
-
Serilog.Web.Classic -
WithHttpRequestId()
and many other enrichers useful in classic ASP.NET applications -
Serilog.Exceptions -
WithExceptionDetails()
adds additional structured properties from exceptions -
Serilog.Enrichers.Demystify -
WithDemystifiedStackTraces()
-
Serilog.Enrichers.ClientInfo -
WithClientIp()
,WithCorrelationId()
andWithRequestHeader("header-name")
will add properties with client IP, correlation id and HTTP request header value -
Serilog.Enrichers.ExcelDna -
WithXllPath()
and many other enrichers useful in Excel-DNA add-ins -
Serilog.Enrichers.Sensitive -
WithSensitiveDataMasking()
masks sensitive data in log events -
Serilog.Enrichers.GlobalLogContext -
FromGlobalLogContext()
adds properties from the "global context" that can be added dynamically