From 44a6e28497c11d7630dc44edb8c9a46b4a4b0afb Mon Sep 17 00:00:00 2001 From: Richard Garcia Date: Wed, 11 Mar 2026 22:50:52 -0300 Subject: [PATCH] feature(#14): this commit enriches the logging context by adding `user_id` and `user_name` to the serilog logging context and as tags in sentry --- .../Middlewares/PrincipalMiddleware.cs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/Boundaries/Comanda.Orchestrator/Source/Comanda.Orchestrator.WebApi/Middlewares/PrincipalMiddleware.cs b/Boundaries/Comanda.Orchestrator/Source/Comanda.Orchestrator.WebApi/Middlewares/PrincipalMiddleware.cs index feb4a13..731a0cd 100644 --- a/Boundaries/Comanda.Orchestrator/Source/Comanda.Orchestrator.WebApi/Middlewares/PrincipalMiddleware.cs +++ b/Boundaries/Comanda.Orchestrator/Source/Comanda.Orchestrator.WebApi/Middlewares/PrincipalMiddleware.cs @@ -17,7 +17,7 @@ public async Task InvokeAsync(HttpContext context) return; } - var userName = context.User.FindFirst(ClaimTypes.Name); + var userName = context.User.Claims.FirstOrDefault(claim => claim.Type == "preferred_username"); var userId = context.User.FindFirst(ClaimTypes.NameIdentifier); if (userId == null || string.IsNullOrWhiteSpace(userId.Value)) @@ -34,6 +34,21 @@ public async Task InvokeAsync(HttpContext context) principalProvider.SetPrincipal(new User(userId.Value, userName.Value)); - await next(context); + /* enriches logging and monitoring contexts with user information */ + /* enabling traceability of user actions across logs and error monitoring tools */ + + using (LogContext.PushProperty("user_id", userId.Value)) + using (LogContext.PushProperty("user_name", userName.Value)) + + using (SentrySdk.PushScope()) + { + SentrySdk.ConfigureScope(scope => + { + scope.SetTag("user_id", userId.Value); + scope.SetTag("user_name", userName.Value); + }); + + await next(context); + } } }