|
1 | 1 | using System.Diagnostics; |
| 2 | +using System.Security.Claims; |
2 | 3 | using System.Text; |
3 | 4 |
|
4 | 5 | using Arbiter.Services; |
@@ -59,15 +60,19 @@ public async Task InvokeAsync(HttpContext context) |
59 | 60 | return; |
60 | 61 | } |
61 | 62 |
|
62 | | - // track user identity in logs and tracing |
63 | | - var userName = context.User?.Identity?.Name ?? "anonymous"; |
| 63 | + var (UserName, UserId) = ResolveUserContext(context); |
64 | 64 |
|
65 | | - // enrich logs with user information |
66 | | - using var scope = _logger.BeginScope("{UserName}", userName); |
| 65 | + // enrich logs with user information for downstream logs |
| 66 | + // use an array (IReadOnlyList<KeyValuePair<,>>) instead of Dictionary to avoid hashing/bucket allocation |
| 67 | + using var scope = _logger.BeginScope(new[] |
| 68 | + { |
| 69 | + new KeyValuePair<string, object?>("UserName", UserName), |
| 70 | + new KeyValuePair<string, object?>("UserId", UserId), |
| 71 | + }); |
67 | 72 |
|
68 | 73 | // enrich tracing with user information |
69 | | - Activity.Current?.SetTag("enduser.name", userName); |
70 | | - |
| 74 | + Activity.Current?.SetTag("enduser.name", UserName); |
| 75 | + Activity.Current?.SetTag("enduser.id", UserId); |
71 | 76 |
|
72 | 77 | // start timing |
73 | 78 | var timestamp = Stopwatch.GetTimestamp(); |
@@ -203,6 +208,41 @@ private bool ShouldLogBody(HttpRequest request) |
203 | 208 | } |
204 | 209 | } |
205 | 210 |
|
| 211 | + |
| 212 | + private static (string UserName, string UserId) ResolveUserContext(HttpContext context) |
| 213 | + { |
| 214 | + var user = context.User; |
| 215 | + if (user?.Identity?.IsAuthenticated != true) |
| 216 | + return ("anonymous", "anonymous"); |
| 217 | + |
| 218 | + var userName = ResolveUserName(user); |
| 219 | + var userId = ResolveUserId(user); |
| 220 | + |
| 221 | + return (userName, userId); |
| 222 | + } |
| 223 | + |
| 224 | + private static string ResolveUserName(ClaimsPrincipal user) |
| 225 | + { |
| 226 | + var userName = user.Identity?.Name; |
| 227 | + if (!string.IsNullOrWhiteSpace(userName)) |
| 228 | + return userName; |
| 229 | + |
| 230 | + return user.FindFirstValue(ClaimTypes.Name) |
| 231 | + ?? user.FindFirstValue(ClaimTypes.Upn) |
| 232 | + ?? user.FindFirstValue(ClaimTypes.Email) |
| 233 | + ?? user.FindFirstValue(ClaimTypes.NameIdentifier) |
| 234 | + ?? "anonymous"; |
| 235 | + } |
| 236 | + |
| 237 | + private static string ResolveUserId(ClaimsPrincipal user) |
| 238 | + { |
| 239 | + return user.FindFirstValue(ClaimTypes.NameIdentifier) |
| 240 | + ?? user.FindFirstValue("sub") |
| 241 | + ?? user.FindFirstValue("oid") |
| 242 | + ?? ResolveUserName(user); |
| 243 | + } |
| 244 | + |
| 245 | + |
206 | 246 | [LoggerMessage(EventId = 1, Message = "HTTP {RequestMethod} {RequestPath} responded {StatusCode} in {Elapsed:0.0000} ms")] |
207 | 247 | private static partial void LogRequestBasic(ILogger logger, LogLevel logLevel, string? requestMethod, string? requestPath, int statusCode, double elapsed); |
208 | 248 |
|
|
0 commit comments