Skip to content

Commit

Permalink
Merge pull request #6 from biplovkc/biplovkc-patch-1
Browse files Browse the repository at this point in the history
Update HttpContextEnricher.cs
  • Loading branch information
biplovkc authored Jun 4, 2023
2 parents d85269f + 209e8ad commit a1bbe46
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/HttpContextEnricher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,35 @@ public static void HttpRequestEnricher(IDiagnosticContext diagnosticContext, Htt
.GetMetadata<RouteNameMetadata>()?.RouteName
};
var userAgent = httpContext.Request.Headers?.FirstOrDefault(s => "user-agent".Equals(s.Key, StringComparison.OrdinalIgnoreCase)).Value;
httpContextInfo.UserAgent = userAgent is not null ? userAgent.ToString() : "";
// get a parser with the embedded regex patterns
var uaParser = Parser.GetDefault();
httpContextInfo.UserAgent = userAgent.ToString();


// get a parser using externally supplied yaml definitions
// var uaParser = Parser.FromYaml(yamlString);

var clientInfo = uaParser.Parse(userAgent);
if (!string.IsNullOrWhiteSpace(httpContextInfo.UserAgent))
{
// get a parser with the embedded regex patterns
var uaParser = Parser.GetDefault();
var clientInfo = uaParser.Parse(httpContextInfo.UserAgent);

if (!string.IsNullOrWhiteSpace(clientInfo?.Device?.Family))
diagnosticContext.Set("Device", clientInfo.Device.Family);

if (!string.IsNullOrWhiteSpace(clientInfo?.OS?.Family))
diagnosticContext.Set("OperatingSystem", clientInfo.OS.Family);

if (!string.IsNullOrWhiteSpace(clientInfo?.UA?.Family))
diagnosticContext.Set("Browser", clientInfo.UA.Family);
}

diagnosticContext.Set("Route", httpContextInfo.Route);
diagnosticContext.Set("User", httpContextInfo.User);
diagnosticContext.Set("Host", httpContextInfo.Host);
diagnosticContext.Set("IpAddress", httpContextInfo.IpAddress);
diagnosticContext.Set("Protocol", httpContextInfo.Protocol);
diagnosticContext.Set("Scheme", httpContextInfo.Scheme);
if (!string.IsNullOrWhiteSpace(clientInfo?.Device?.Family))
diagnosticContext.Set("Device", clientInfo.Device.Family);

if (!string.IsNullOrWhiteSpace(clientInfo?.OS?.Family))
diagnosticContext.Set("OperatingSystem", clientInfo.OS.Family);

if (!string.IsNullOrWhiteSpace(clientInfo?.UA?.Family))
diagnosticContext.Set("Browser", clientInfo.UA.Family);
}

private static string GetUserInfo(IPrincipal user) => user.Identity is { IsAuthenticated: true } ? user.Identity.Name : Environment.UserName;
Expand Down

0 comments on commit a1bbe46

Please sign in to comment.