Skip to content

Commit c73df50

Browse files
merge pull request #15 from https-richardy/feature/014-logging-context-authenticated-user
[#14] - enriches the logging context by adding authenticated user information
2 parents 92a3160 + 44a6e28 commit c73df50

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

Boundaries/Comanda.Orchestrator/Source/Comanda.Orchestrator.WebApi/Middlewares/PrincipalMiddleware.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public async Task InvokeAsync(HttpContext context)
1717
return;
1818
}
1919

20-
var userName = context.User.FindFirst(ClaimTypes.Name);
20+
var userName = context.User.Claims.FirstOrDefault(claim => claim.Type == "preferred_username");
2121
var userId = context.User.FindFirst(ClaimTypes.NameIdentifier);
2222

2323
if (userId == null || string.IsNullOrWhiteSpace(userId.Value))
@@ -34,6 +34,21 @@ public async Task InvokeAsync(HttpContext context)
3434

3535
principalProvider.SetPrincipal(new User(userId.Value, userName.Value));
3636

37-
await next(context);
37+
/* enriches logging and monitoring contexts with user information */
38+
/* enabling traceability of user actions across logs and error monitoring tools */
39+
40+
using (LogContext.PushProperty("user_id", userId.Value))
41+
using (LogContext.PushProperty("user_name", userName.Value))
42+
43+
using (SentrySdk.PushScope())
44+
{
45+
SentrySdk.ConfigureScope(scope =>
46+
{
47+
scope.SetTag("user_id", userId.Value);
48+
scope.SetTag("user_name", userName.Value);
49+
});
50+
51+
await next(context);
52+
}
3853
}
3954
}

0 commit comments

Comments
 (0)