From bda46c55686e5185015931af8a383de30e8836bb Mon Sep 17 00:00:00 2001 From: BiplovKC Date: Fri, 2 Jun 2023 19:57:19 +0200 Subject: [PATCH 1/2] Update HttpContextEnricher.cs --- src/HttpContextEnricher.cs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/HttpContextEnricher.cs b/src/HttpContextEnricher.cs index c9ad782..15948d1 100644 --- a/src/HttpContextEnricher.cs +++ b/src/HttpContextEnricher.cs @@ -1,7 +1,9 @@ -using Microsoft.AspNetCore.Http.Features; +using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Routing; using System.Security.Principal; +using UAParser; + namespace Biplov.Serilog; public static class HttpContextEnricher @@ -20,9 +22,24 @@ public static void HttpRequestEnricher(IDiagnosticContext diagnosticContext, Htt }; var userAgent = httpContext.Request.Headers?.FirstOrDefault(s => "user-agent".Equals(s.Key, StringComparison.OrdinalIgnoreCase)).Value; httpContextInfo.UserAgent = userAgent is not null ? userAgent.ToString() : ""; - - diagnosticContext.Set("HttpContext", httpContextInfo, true); + // get a parser with the embedded regex patterns + var uaParser = Parser.GetDefault(); + + // get a parser using externally supplied yaml definitions + // var uaParser = Parser.FromYaml(yamlString); + + var clientInfo = uaParser.Parse(userAgent); + + 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); + diagnosticContext.Set("Device", clientInfo.Device.Family); + diagnosticContext.Set("OperatingSystem", clientInfo.OS.Family); + diagnosticContext.Set("Browser", clientInfo.UA.Family); } private static string GetUserInfo(IPrincipal user) => user.Identity is { IsAuthenticated: true } ? user.Identity.Name : Environment.UserName; -} \ No newline at end of file +} From 3da0972f7f0eee175dc3f01fb6222a639b21ae48 Mon Sep 17 00:00:00 2001 From: BiplovKC Date: Fri, 2 Jun 2023 19:58:27 +0200 Subject: [PATCH 2/2] Update Biplov.Serilog.csproj --- src/Biplov.Serilog.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Biplov.Serilog.csproj b/src/Biplov.Serilog.csproj index 991e1e0..1ad7f72 100644 --- a/src/Biplov.Serilog.csproj +++ b/src/Biplov.Serilog.csproj @@ -42,6 +42,7 @@ +