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 @@ + 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 +}