|
1 | | -using Microsoft.Net.Http.Headers; |
| 1 | +using System.Security.Claims; |
| 2 | +using Microsoft.Net.Http.Headers; |
| 3 | +using OpenShock.Common.Authentication; |
2 | 4 | using Serilog; |
3 | 5 | using Serilog.Configuration; |
4 | 6 | using Serilog.Core; |
@@ -32,16 +34,25 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) |
32 | 34 | logEvent.AddOrUpdateProperty(new LogEventProperty("CF-IPCountry", new ScalarValue(ctx.GetCFIPCountry()))); |
33 | 35 |
|
34 | 36 | //logEvent.AddOrUpdateProperty(new LogEventProperty("Headers", new DictionaryValue(ctx.Request.Headers.Select(x => new KeyValuePair<ScalarValue, LogEventPropertyValue>(new ScalarValue(x.Key), new ScalarValue(x.Value)))))); |
35 | | - |
36 | | - TryAddVar(logEvent, ctx, "User"); |
37 | | - TryAddVar(logEvent, ctx, "Device"); |
| 37 | + |
| 38 | + foreach (var claim in ctx.User.Claims) |
| 39 | + { |
| 40 | + switch (claim.Type) |
| 41 | + { |
| 42 | + case ClaimTypes.NameIdentifier: |
| 43 | + AddVar(logEvent, "User", claim.Value); |
| 44 | + break; |
| 45 | + case OpenShockAuthClaims.ApiTokenId: |
| 46 | + AddVar(logEvent, "ApiToken", claim.Value); |
| 47 | + break; |
| 48 | + |
| 49 | + } |
| 50 | + } |
38 | 51 | } |
39 | 52 |
|
40 | | - private void TryAddVar(LogEvent logEvent, HttpContext ctx, string name) |
| 53 | + private void AddVar(LogEvent logEvent, string key, string value) |
41 | 54 | { |
42 | | - var user = ctx.Items[name]; |
43 | | - if (user == null) return; |
44 | | - var propertyId = new LogEventProperty(name, new ScalarValue(user)); |
| 55 | + var propertyId = new LogEventProperty(key, new ScalarValue(value)); |
45 | 56 | logEvent.AddOrUpdateProperty(propertyId); |
46 | 57 | } |
47 | 58 | } |
|
0 commit comments