Skip to content

Commit 7ea93df

Browse files
committed
Merge branch 'master' into develop
2 parents ed401ba + 6dd5f35 commit 7ea93df

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

Common/Utils/OpenShockEnricher.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using Microsoft.Net.Http.Headers;
1+
using System.Security.Claims;
2+
using Microsoft.Net.Http.Headers;
3+
using OpenShock.Common.Authentication;
24
using Serilog;
35
using Serilog.Configuration;
46
using Serilog.Core;
@@ -32,16 +34,25 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
3234
logEvent.AddOrUpdateProperty(new LogEventProperty("CF-IPCountry", new ScalarValue(ctx.GetCFIPCountry())));
3335

3436
//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+
}
3851
}
3952

40-
private void TryAddVar(LogEvent logEvent, HttpContext ctx, string name)
53+
private void AddVar(LogEvent logEvent, string key, string value)
4154
{
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));
4556
logEvent.AddOrUpdateProperty(propertyId);
4657
}
4758
}

0 commit comments

Comments
 (0)