Enriches Serilog events with information from the ClaimsPrincipal.
Install the Serilog.Enrichers.AzureClaims NuGet package
Install the Serilog.Enrichers.Claims NuGet package
Install-Package Serilog.Enrichers.AzureClaims
Install-Package Serilog.Enrichers.ClaimsThen, apply the enricher to your LoggerConfiguration:
Log.Logger = new LoggerConfiguration()
.Enrich.WithUpn()
.Enrich.WithDisplayName()
.Enrich.WithTenantId()
.Enrich.WithObjectId()
.Enrich.WithAppId()
.Enrich.WithCustomClaim("AnyExistingClaim") // Available from Serilog.Enrichers.Claims
// ...other configuration...
.CreateLogger();WithUpn()- addsUserPrincipalNamebased on the ClaimTypehttp://schemas.xmlsoap.org/ws/2005/05/identity/claims/upnWithDisplayName()- addsDisplayNamebased on the ClaimTypehttp://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameornameorpreferred_usernameWithTenantId()- addsTenantIdbased on the ClaimTypehttp://schemas.microsoft.com/identity/claims/tenantidortidWithObjectId()- addsObjectIdbased on the ClaimTypehttp://schemas.microsoft.com/identity/claims/objectidentifieroroidWithAppId()- addsAppIdbased on the CLaimTypeappidorazp
WithCustomClaim("AnyExistingClaim")based on the claim you want to extract from the ClaimsPrincipal
The IHttpContextAccessor singleton should be registered, but is not required for these nugets to run. The enrichers have access to the requests HttpContext to extract the data.
This is what your Program class should contain in order for this enricher to work as expected:
// ...
using Serilog;
var builder = WebApplication.CreateBuilder(args);
builder.Logging.AddSerilog(new LoggerConfiguration()
.Enrich.WithUpn()
.Enrich.WithDisplayName()
.Enrich.WithTenantId()
.Enrich.WithObjectId()
.Enrich.WithAppId()
.Enrich.WithCustomClaim("AnyExistingClaim")
.CreateLogger());
var app = builder.Build();
app.UseSerilogRequestLogging();
// ...