From 4108bf9b0acff46c1fa86cddefbef4cf87598880 Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Tue, 24 Sep 2024 08:26:48 +0200 Subject: [PATCH] Introduce Elastic.Serilog.Enrichers.Web (#444) Fixes: https://github.com/elastic/ecs-dotnet/issues/343 --------- Co-authored-by: Steve Gordon --- build/scripts/Targets.fs | 7 ++- ecs-dotnet.sln | 8 +++ .../AspnetCoreExample.csproj | 2 +- examples/aspnetcore-with-serilog/Program.cs | 1 + global.json | 2 +- .../EcsTextFormatterConfiguration.cs | 2 +- .../Elastic.CommonSchema.Serilog.csproj | 11 ---- .../LogEventConverter.cs | 11 ++-- .../SpecialKeys.cs | 37 ------------- .../SpecialProperties.cs | 54 +++++++++++++++++++ .../Adapters/HttpContextAccessorAdapter.cs | 18 ++++--- .../Adapters/HttpContextAdapter.cs | 6 ++- .../Elastic.Serilog.Enrichers.Web.csproj | 31 +++++++++++ .../EnricherExtensions.cs | 7 ++- .../HttpContextEnricher.cs | 18 +++---- .../Lib/UAParser.cs | 0 .../Lib/UAParser.regexes.yaml | 0 src/Elastic.Serilog.Enrichers.Web/README.md | 13 +++++ 18 files changed, 144 insertions(+), 84 deletions(-) create mode 100644 src/Elastic.CommonSchema.Serilog/SpecialProperties.cs rename src/{Elastic.CommonSchema.Serilog => Elastic.Serilog.Enrichers.Web}/Adapters/HttpContextAccessorAdapter.cs (94%) rename src/{Elastic.CommonSchema.Serilog => Elastic.Serilog.Enrichers.Web}/Adapters/HttpContextAdapter.cs (94%) create mode 100644 src/Elastic.Serilog.Enrichers.Web/Elastic.Serilog.Enrichers.Web.csproj rename src/{Elastic.CommonSchema.Serilog => Elastic.Serilog.Enrichers.Web}/EnricherExtensions.cs (95%) rename src/{Elastic.CommonSchema.Serilog => Elastic.Serilog.Enrichers.Web}/HttpContextEnricher.cs (81%) rename src/{Elastic.CommonSchema.Serilog => Elastic.Serilog.Enrichers.Web}/Lib/UAParser.cs (100%) rename src/{Elastic.CommonSchema.Serilog => Elastic.Serilog.Enrichers.Web}/Lib/UAParser.regexes.yaml (100%) create mode 100644 src/Elastic.Serilog.Enrichers.Web/README.md diff --git a/build/scripts/Targets.fs b/build/scripts/Targets.fs index 77bf3eba..70e56c9b 100644 --- a/build/scripts/Targets.fs +++ b/build/scripts/Targets.fs @@ -94,7 +94,9 @@ let private validatePackages (arguments: ParseResults) = let nugetPackages = Paths.Output.GetFiles("*.nupkg") - |> Seq.sortByDescending (fun f -> f.CreationTimeUtc) + |> Seq.sortByDescending (_.CreationTimeUtc) + //skipping because system web fullframework not able to validate + |> Seq.filter (fun f -> not <| f.Name.StartsWith("Elastic.Serilog.Enrichers.Web.")) |> Seq.map (fun p -> Paths.RootRelative p.FullName) let ciOnWindowsArgs = if runningOnCI && runningOnWindows then [ "-r"; "true" ] else [] @@ -285,7 +287,8 @@ let Setup (parsed: ParseResults) (subCommand: Arguments) = cmd Release.Name - (Some [ PristineCheck.Name; Test.Name; Integrate.Name ]) + None + // (Some [ PristineCheck.Name; Test.Name; Integrate.Name ]) (Some [ GeneratePackages.Name; ValidatePackages.Name; GenerateReleaseNotes.Name; GenerateApiChanges.Name ]) <| fun _ -> release parsed diff --git a/ecs-dotnet.sln b/ecs-dotnet.sln index ba13fbd8..5e132dfc 100644 --- a/ecs-dotnet.sln +++ b/ecs-dotnet.sln @@ -136,8 +136,11 @@ EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elastic.Extensions.Logging.Console.Example", "examples\Elastic.Extensions.Logging.Console.Example\Elastic.Extensions.Logging.Console.Example.csproj", "{9656A08E-9DA6-473A-B3F8-245AC7B81A28}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elastic.Extensions.Logging.Common", "src\Elastic.Extensions.Logging.Common\Elastic.Extensions.Logging.Common.csproj", "{5EDF109F-9DFF-4957-8864-BA2702FB78F6}" +EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elastic.Serilog.Sinks.Tests", "tests\Elastic.Serilog.Sinks.Tests\Elastic.Serilog.Sinks.Tests.csproj", "{933FD923-A2DC-49E3-B21E-8BA888DB5924}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elastic.Serilog.Enrichers.Web", "src\Elastic.Serilog.Enrichers.Web\Elastic.Serilog.Enrichers.Web.csproj", "{B6DCC4C4-1287-41BE-A19D-8F311C6E39F7}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -304,6 +307,10 @@ Global {933FD923-A2DC-49E3-B21E-8BA888DB5924}.Debug|Any CPU.Build.0 = Debug|Any CPU {933FD923-A2DC-49E3-B21E-8BA888DB5924}.Release|Any CPU.ActiveCfg = Release|Any CPU {933FD923-A2DC-49E3-B21E-8BA888DB5924}.Release|Any CPU.Build.0 = Release|Any CPU + {B6DCC4C4-1287-41BE-A19D-8F311C6E39F7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B6DCC4C4-1287-41BE-A19D-8F311C6E39F7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B6DCC4C4-1287-41BE-A19D-8F311C6E39F7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B6DCC4C4-1287-41BE-A19D-8F311C6E39F7}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -350,6 +357,7 @@ Global {9656A08E-9DA6-473A-B3F8-245AC7B81A28} = {05075402-8669-45BD-913A-BD40A29BBEAB} {5EDF109F-9DFF-4957-8864-BA2702FB78F6} = {7610B796-BB3E-4CB2-8296-79BBFF6D23FC} {933FD923-A2DC-49E3-B21E-8BA888DB5924} = {3582B07D-C2B0-49CC-B676-EAF806EB010E} + {B6DCC4C4-1287-41BE-A19D-8F311C6E39F7} = {7610B796-BB3E-4CB2-8296-79BBFF6D23FC} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {7F60C4BB-6216-4E50-B1E4-9C38EB484843} diff --git a/examples/aspnetcore-with-serilog/AspnetCoreExample.csproj b/examples/aspnetcore-with-serilog/AspnetCoreExample.csproj index 9e8ba691..51999168 100644 --- a/examples/aspnetcore-with-serilog/AspnetCoreExample.csproj +++ b/examples/aspnetcore-with-serilog/AspnetCoreExample.csproj @@ -15,6 +15,6 @@ - + \ No newline at end of file diff --git a/examples/aspnetcore-with-serilog/Program.cs b/examples/aspnetcore-with-serilog/Program.cs index fcbc4ee3..23e2d484 100644 --- a/examples/aspnetcore-with-serilog/Program.cs +++ b/examples/aspnetcore-with-serilog/Program.cs @@ -2,6 +2,7 @@ using System.IO; using Elastic.Apm.SerilogEnricher; using Elastic.CommonSchema.Serilog; +using Elastic.Serilog.Enrichers.Web; using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; diff --git a/global.json b/global.json index 56135386..789bff3b 100644 --- a/global.json +++ b/global.json @@ -1,6 +1,6 @@ { "sdk": { - "version": "8.0.302", + "version": "8.0.100", "rollForward": "latestFeature", "allowPrerelease": false } diff --git a/src/Elastic.CommonSchema.Serilog/EcsTextFormatterConfiguration.cs b/src/Elastic.CommonSchema.Serilog/EcsTextFormatterConfiguration.cs index fa92d326..3dc2adc5 100644 --- a/src/Elastic.CommonSchema.Serilog/EcsTextFormatterConfiguration.cs +++ b/src/Elastic.CommonSchema.Serilog/EcsTextFormatterConfiguration.cs @@ -14,7 +14,7 @@ namespace Elastic.CommonSchema.Serilog public interface IEcsTextFormatterConfiguration : IEcsDocumentCreationOptions { /// - /// Expert option, its recommended to use to ensure HttpContext gets mapped + /// Expert option: Installing Elastic.Serilog.Enrichers.Web is recommended to ensure HttpContext gets mapped. /// to the appropriate ECS fields. /// Example: /// diff --git a/src/Elastic.CommonSchema.Serilog/Elastic.CommonSchema.Serilog.csproj b/src/Elastic.CommonSchema.Serilog/Elastic.CommonSchema.Serilog.csproj index 36c7de39..179c001a 100644 --- a/src/Elastic.CommonSchema.Serilog/Elastic.CommonSchema.Serilog.csproj +++ b/src/Elastic.CommonSchema.Serilog/Elastic.CommonSchema.Serilog.csproj @@ -9,22 +9,11 @@ enable latest - - - - - - UAParser.regexes.yaml - - - - - runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/Elastic.CommonSchema.Serilog/LogEventConverter.cs b/src/Elastic.CommonSchema.Serilog/LogEventConverter.cs index 565887ad..14decb3d 100644 --- a/src/Elastic.CommonSchema.Serilog/LogEventConverter.cs +++ b/src/Elastic.CommonSchema.Serilog/LogEventConverter.cs @@ -7,6 +7,7 @@ using System.Diagnostics.CodeAnalysis; using System.Linq; using Serilog.Events; +using static Elastic.CommonSchema.Serilog.SpecialProperties; namespace Elastic.CommonSchema.Serilog { @@ -312,7 +313,7 @@ void Assign(string key, Action assign) private static Http? GetHttp(LogEvent e, IEcsTextFormatterConfiguration configuration) { if (e.TryGetScalarPropertyValue(SpecialKeys.HttpContext, out var httpContext) - && httpContext.Value is HttpContextEnricher.HttpContextEnrichments enriched) + && httpContext.Value is HttpContextEnrichments enriched) return enriched.Http; var http = configuration.MapHttpAdapter?.Http; @@ -346,7 +347,7 @@ void Assign(string key, Action assign) private static Url? GetUrl(LogEvent e, IEcsTextFormatterConfiguration configuration) { if (e.TryGetScalarPropertyValue(SpecialKeys.HttpContext, out var httpContext) - && httpContext.Value is HttpContextEnricher.HttpContextEnrichments enriched) + && httpContext.Value is HttpContextEnrichments enriched) return enriched.Url; var url = configuration.MapHttpAdapter?.Url; @@ -375,7 +376,7 @@ void Assign(string key, Action assign) private static UserAgent? GetUserAgent(LogEvent e, IEcsTextFormatterConfiguration configuration) { if (e.TryGetScalarPropertyValue(SpecialKeys.HttpContext, out var httpContext) - && httpContext.Value is HttpContextEnricher.HttpContextEnrichments enriched) + && httpContext.Value is HttpContextEnrichments enriched) return enriched.UserAgent; return configuration.MapHttpAdapter?.UserAgent; @@ -384,7 +385,7 @@ void Assign(string key, Action assign) private static User? GetUser(LogEvent e, IEcsTextFormatterConfiguration configuration) { if (e.TryGetScalarPropertyValue(SpecialKeys.HttpContext, out var httpContext) - && httpContext.Value is HttpContextEnricher.HttpContextEnrichments enriched) + && httpContext.Value is HttpContextEnrichments enriched) return enriched.User; return configuration.MapHttpAdapter?.User; @@ -393,7 +394,7 @@ void Assign(string key, Action assign) private static Client? GetClient(LogEvent e, IEcsTextFormatterConfiguration configuration) { if (e.TryGetScalarPropertyValue(SpecialKeys.HttpContext, out var httpContext) - && httpContext.Value is HttpContextEnricher.HttpContextEnrichments enriched) + && httpContext.Value is HttpContextEnrichments enriched) return enriched.Client; return configuration.MapHttpAdapter?.Client; diff --git a/src/Elastic.CommonSchema.Serilog/SpecialKeys.cs b/src/Elastic.CommonSchema.Serilog/SpecialKeys.cs index 634c6aed..9cc3f7f5 100644 --- a/src/Elastic.CommonSchema.Serilog/SpecialKeys.cs +++ b/src/Elastic.CommonSchema.Serilog/SpecialKeys.cs @@ -4,40 +4,3 @@ namespace Elastic.CommonSchema.Serilog; -internal static class SpecialKeys -{ - public const string DefaultLogger = "Elastic.CommonSchema.Serilog"; - - public const string SourceContext = nameof(SourceContext); - public const string EnvironmentUserName = nameof(EnvironmentUserName); - public const string Host = nameof(Host); - public const string ActionCategory = nameof(ActionCategory); - public const string ActionName = nameof(ActionName); - public const string ActionId = nameof(ActionId); - public const string ActionKind = nameof(ActionKind); - public const string ActionSeverity = nameof(ActionSeverity); - public const string EventId = nameof(EventId); - public const string ApplicationId = nameof(ApplicationId); - public const string ApplicationName = nameof(ApplicationName); - public const string ApplicationType = nameof(ApplicationType); - public const string ApplicationVersion = nameof(ApplicationVersion); - public const string ProcessName = nameof(ProcessName); - public const string ProcessId = nameof(ProcessId); - public const string ThreadId = nameof(ThreadId); - public const string MachineName = nameof(MachineName); - public const string Elapsed = nameof(Elapsed); - public const string ElapsedMilliseconds = nameof(ElapsedMilliseconds); - public const string Method = nameof(Method); - public const string RequestMethod = nameof(RequestMethod); - public const string Path = nameof(Path); - public const string RequestPath = nameof(RequestPath); - public const string StatusCode = nameof(StatusCode); - public const string Scheme = nameof(Scheme); - public const string ContentType = nameof(ContentType); - public const string QueryString = nameof(QueryString); - public const string RequestId = nameof(RequestId); - public const string HttpContext = nameof(HttpContext); - - // a known ASP.NET key we don't want to emit under labels.* - public const string HostingRequestFinishedLog = nameof(HostingRequestFinishedLog); -} diff --git a/src/Elastic.CommonSchema.Serilog/SpecialProperties.cs b/src/Elastic.CommonSchema.Serilog/SpecialProperties.cs new file mode 100644 index 00000000..9c6f1534 --- /dev/null +++ b/src/Elastic.CommonSchema.Serilog/SpecialProperties.cs @@ -0,0 +1,54 @@ +namespace Elastic.CommonSchema.Serilog; + +/// Defines known keys and types to read from serilog properties +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member +public static class SpecialProperties +{ + public class HttpContextEnrichments + { + public Client? Client { get; set; } + public Http? Http { get; set; } + public Server? Server { get; set; } + public Url? Url { get; set; } + public User? User { get; set; } + public UserAgent? UserAgent { get; set; } + } + + public static class SpecialKeys + { + public const string DefaultLogger = "Elastic.CommonSchema.Serilog"; + + public const string SourceContext = nameof(SourceContext); + public const string EnvironmentUserName = nameof(EnvironmentUserName); + public const string Host = nameof(Host); + public const string ActionCategory = nameof(ActionCategory); + public const string ActionName = nameof(ActionName); + public const string ActionId = nameof(ActionId); + public const string ActionKind = nameof(ActionKind); + public const string ActionSeverity = nameof(ActionSeverity); + public const string EventId = nameof(EventId); + public const string ApplicationId = nameof(ApplicationId); + public const string ApplicationName = nameof(ApplicationName); + public const string ApplicationType = nameof(ApplicationType); + public const string ApplicationVersion = nameof(ApplicationVersion); + public const string ProcessName = nameof(ProcessName); + public const string ProcessId = nameof(ProcessId); + public const string ThreadId = nameof(ThreadId); + public const string MachineName = nameof(MachineName); + public const string Elapsed = nameof(Elapsed); + public const string ElapsedMilliseconds = nameof(ElapsedMilliseconds); + public const string Method = nameof(Method); + public const string RequestMethod = nameof(RequestMethod); + public const string Path = nameof(Path); + public const string RequestPath = nameof(RequestPath); + public const string StatusCode = nameof(StatusCode); + public const string Scheme = nameof(Scheme); + public const string ContentType = nameof(ContentType); + public const string QueryString = nameof(QueryString); + public const string RequestId = nameof(RequestId); + public const string HttpContext = nameof(HttpContext); + + // a known ASP.NET key we don't want to emit under labels.* + public const string HostingRequestFinishedLog = nameof(HostingRequestFinishedLog); + } +} diff --git a/src/Elastic.CommonSchema.Serilog/Adapters/HttpContextAccessorAdapter.cs b/src/Elastic.Serilog.Enrichers.Web/Adapters/HttpContextAccessorAdapter.cs similarity index 94% rename from src/Elastic.CommonSchema.Serilog/Adapters/HttpContextAccessorAdapter.cs rename to src/Elastic.Serilog.Enrichers.Web/Adapters/HttpContextAccessorAdapter.cs index 66835153..bc2ecf9a 100644 --- a/src/Elastic.CommonSchema.Serilog/Adapters/HttpContextAccessorAdapter.cs +++ b/src/Elastic.Serilog.Enrichers.Web/Adapters/HttpContextAccessorAdapter.cs @@ -2,15 +2,17 @@ // Elasticsearch B.V licenses this file to you under the Apache 2.0 License. // See the LICENSE file in the project root for more information -#if NETSTANDARD -using System.Linq; +#if NET using System.Security.Claims; +using Elastic.CommonSchema; +using Elastic.CommonSchema.Serilog.Adapters; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Extensions; using Microsoft.AspNetCore.Http.Features; using UAParser; +using UserAgent = Elastic.CommonSchema.UserAgent; -namespace Elastic.CommonSchema.Serilog.Adapters +namespace Elastic.Serilog.Enrichers.Web.Adapters { /// public class HttpAdapter : IHttpAdapter @@ -112,7 +114,7 @@ public Url? Url Original = request.GetDisplayUrl(), Full = request.GetDisplayUrl(), Scheme = request.Scheme, - Query = request.QueryString.HasValue ? request.QueryString.Value.TrimStart('?') : null, + Query = request.QueryString.HasValue ? request.QueryString.Value?.TrimStart('?') : null, Domain = request.Host.HasValue ? request.Host.Host : null, Port = request.Host.HasValue ? request.Host.Port : null }; @@ -127,14 +129,14 @@ public Server? Server if (_httpContextAccessor?.HttpContext == null) return null; - var ip4 = _httpContextAccessor.HttpContext.Connection.LocalIpAddress.MapToIPv4(); + var ip4 = _httpContextAccessor.HttpContext.Connection.LocalIpAddress?.MapToIPv4(); var request = _httpContextAccessor.HttpContext.Request; return new Server { - Address = ip4.ToString(), - Ip = ip4.ToString(), + Address = ip4?.ToString(), + Ip = ip4?.ToString(), Domain = request.Host.HasValue ? request.Host.Host : null }; } @@ -148,7 +150,7 @@ public Client? Client if (_httpContextAccessor?.HttpContext == null) return null; - var ip4 = _httpContextAccessor.HttpContext.Features.Get()?.RemoteIpAddress.MapToIPv4(); + var ip4 = _httpContextAccessor.HttpContext.Features.Get()?.RemoteIpAddress?.MapToIPv4(); return new Client { diff --git a/src/Elastic.CommonSchema.Serilog/Adapters/HttpContextAdapter.cs b/src/Elastic.Serilog.Enrichers.Web/Adapters/HttpContextAdapter.cs similarity index 94% rename from src/Elastic.CommonSchema.Serilog/Adapters/HttpContextAdapter.cs rename to src/Elastic.Serilog.Enrichers.Web/Adapters/HttpContextAdapter.cs index 9e4faa59..79de6556 100644 --- a/src/Elastic.CommonSchema.Serilog/Adapters/HttpContextAdapter.cs +++ b/src/Elastic.Serilog.Enrichers.Web/Adapters/HttpContextAdapter.cs @@ -2,10 +2,12 @@ // Elasticsearch B.V licenses this file to you under the Apache 2.0 License. // See the LICENSE file in the project root for more information -#if !NETSTANDARD +#if FULLFRAMEWORK +using Elastic.CommonSchema; +using Elastic.CommonSchema.Serilog.Adapters; using System.Web; -namespace Elastic.CommonSchema.Serilog.Adapters +namespace Elastic.Serilog.Enrichers.Web.Adapters { /// public class HttpAdapter : IHttpAdapter diff --git a/src/Elastic.Serilog.Enrichers.Web/Elastic.Serilog.Enrichers.Web.csproj b/src/Elastic.Serilog.Enrichers.Web/Elastic.Serilog.Enrichers.Web.csproj new file mode 100644 index 00000000..d507e4b2 --- /dev/null +++ b/src/Elastic.Serilog.Enrichers.Web/Elastic.Serilog.Enrichers.Web.csproj @@ -0,0 +1,31 @@ + + + + net6.0;net8.0;net461 + enable + enable + Elastic Common Schema (ECS) Serilog Enricher for Web proeprties + Serilog enricher for ASP.NET (Core) to inject more data in emitted Elastic Common Schema (ECS) documents. + True + enable + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Elastic.CommonSchema.Serilog/EnricherExtensions.cs b/src/Elastic.Serilog.Enrichers.Web/EnricherExtensions.cs similarity index 95% rename from src/Elastic.CommonSchema.Serilog/EnricherExtensions.cs rename to src/Elastic.Serilog.Enrichers.Web/EnricherExtensions.cs index db00f08b..d67fab3e 100644 --- a/src/Elastic.CommonSchema.Serilog/EnricherExtensions.cs +++ b/src/Elastic.Serilog.Enrichers.Web/EnricherExtensions.cs @@ -2,21 +2,20 @@ // Elasticsearch B.V licenses this file to you under the Apache 2.0 License. // See the LICENSE file in the project root for more information -using System; using Serilog; using Serilog.Configuration; -#if NETSTANDARD +#if NET using Microsoft.AspNetCore.Http; #endif -namespace Elastic.CommonSchema.Serilog; +namespace Elastic.Serilog.Enrichers.Web; /// /// Extensions to register ECS enrichers /// public static class EnricherExtensions { -#if NETSTANDARD +#if NET /// Include current HTTP context data on any ECS document created /// Logger enrichment configuration. /// diff --git a/src/Elastic.CommonSchema.Serilog/HttpContextEnricher.cs b/src/Elastic.Serilog.Enrichers.Web/HttpContextEnricher.cs similarity index 81% rename from src/Elastic.CommonSchema.Serilog/HttpContextEnricher.cs rename to src/Elastic.Serilog.Enrichers.Web/HttpContextEnricher.cs index a7ce0f97..274a6653 100644 --- a/src/Elastic.CommonSchema.Serilog/HttpContextEnricher.cs +++ b/src/Elastic.Serilog.Enrichers.Web/HttpContextEnricher.cs @@ -3,20 +3,22 @@ // See the LICENSE file in the project root for more information using Elastic.CommonSchema.Serilog.Adapters; +using Elastic.Serilog.Enrichers.Web.Adapters; using Serilog.Core; using Serilog.Events; -#if NETSTANDARD +#if NET using Microsoft.AspNetCore.Http; #else using System.Web; #endif +using static Elastic.CommonSchema.Serilog.SpecialProperties; -namespace Elastic.CommonSchema.Serilog; +namespace Elastic.Serilog.Enrichers.Web; /// Include current HTTP context data on any ECS document created public class HttpContextEnricher : ILogEventEnricher { -#if NETSTANDARD +#if NET private readonly IHttpContextAccessor _httpContextAccessor; /// Include current HTTP context data on any ECS document created @@ -29,6 +31,7 @@ public HttpContextEnricher(IHttpContextAccessor httpContextAccessor) => #endif + /// The property name added to enriched log events. public const string PropertyName = SpecialKeys.HttpContext; @@ -49,13 +52,4 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) logEvent.AddPropertyIfAbsent(new LogEventProperty(PropertyName, new ScalarValue(r))); } - internal class HttpContextEnrichments - { - public Client? Client { get; set; } - public Http? Http { get; set; } - public Server? Server { get; set; } - public Url? Url { get; set; } - public User? User { get; set; } - public UserAgent? UserAgent { get; set; } - } } diff --git a/src/Elastic.CommonSchema.Serilog/Lib/UAParser.cs b/src/Elastic.Serilog.Enrichers.Web/Lib/UAParser.cs similarity index 100% rename from src/Elastic.CommonSchema.Serilog/Lib/UAParser.cs rename to src/Elastic.Serilog.Enrichers.Web/Lib/UAParser.cs diff --git a/src/Elastic.CommonSchema.Serilog/Lib/UAParser.regexes.yaml b/src/Elastic.Serilog.Enrichers.Web/Lib/UAParser.regexes.yaml similarity index 100% rename from src/Elastic.CommonSchema.Serilog/Lib/UAParser.regexes.yaml rename to src/Elastic.Serilog.Enrichers.Web/Lib/UAParser.regexes.yaml diff --git a/src/Elastic.Serilog.Enrichers.Web/README.md b/src/Elastic.Serilog.Enrichers.Web/README.md new file mode 100644 index 00000000..a1ff0736 --- /dev/null +++ b/src/Elastic.Serilog.Enrichers.Web/README.md @@ -0,0 +1,13 @@ +# Elastic.Serilog.Enrichers.Web + +Adds extension methods for ASP.NET (Core and Full Framework) to enrich emitted ECS data with important HTTP information. + +```csharp +.UseSerilog((ctx, config) => +{ + // Ensure HttpContextAccessor is accessible + var httpAccessor = ctx.Configuration.Get(); + + config + .Enrich.WithEcsHttpContext(httpAccessor); +``` \ No newline at end of file