Enrich Serilog log events with the request's user Id from ClaimsPrincipal.
To use the enricher, first install the NuGet package:
dotnet add package Serilog.Enrichers.RequestUserId
Then, apply the enricher to your LoggerConfiguration
in code:
Log.Logger = new LoggerConfiguration()
.Enrich.WithRequestUserId()
// ...other configuration...
.CreateLogger()
or in the appsettings.json
file:
{
"Serilog": {
"MinimumLevel": "Information",
"Using": ["Serilog.Enrichers.RequestUserId"],
"Enrich": ["WithRequestUserId"],
"WriteTo": [
{
"Name": "Console"
}
]
}
}
As a result the RequestUserId
property will be added to the log events with the content of ClaisPrincipal
with the key uid
or cid
.
You need to register the IHttpContextAccessor
singleton so that the enricher has access to the ClaimsPrincipal
.