Skip to content

Enrich Serilog log events with the request's user Id from ClaimsPrincipal

License

Notifications You must be signed in to change notification settings

juntossomosmais/serilog-enrichers-request-user-id

Repository files navigation

Serilog.Enrichers.RequestUserId

Enrich Serilog log events with the request's user Id from ClaimsPrincipal.

Coverage Quality Gate Status Nuget

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.

About

Enrich Serilog log events with the request's user Id from ClaimsPrincipal

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published