From 209e8adb020cbbf80d71403233a5bb40d24b98ff Mon Sep 17 00:00:00 2001 From: BiplovKC Date: Sun, 4 Jun 2023 17:29:21 +0200 Subject: [PATCH] Update HttpContextEnricher.cs --- src/HttpContextEnricher.cs | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/HttpContextEnricher.cs b/src/HttpContextEnricher.cs index d0ddbff..f65a106 100644 --- a/src/HttpContextEnricher.cs +++ b/src/HttpContextEnricher.cs @@ -21,14 +21,27 @@ public static void HttpRequestEnricher(IDiagnosticContext diagnosticContext, Htt .GetMetadata()?.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); @@ -36,14 +49,7 @@ public static void HttpRequestEnricher(IDiagnosticContext diagnosticContext, Htt 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;