Skip to content

Commit 44a6e28

Browse files
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
1 parent 0a6b56b commit 44a6e28

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)