From b77551f4b2b1779953a81159ed48c5b8d66ed113 Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Tue, 24 Sep 2024 08:27:14 +0200 Subject: [PATCH] Add MapCustom to Extensions logging (#448) This existed for Serilog but not Extensions Logging. --- .../Elastic.Extensions.Logging.Console.Example.csproj | 2 +- .../Program.cs | 8 ++++++-- .../ILogEventCreationOptions.cs | 10 ++++++++++ .../EcsConsoleFormatter.cs | 6 ++++-- .../EcsConsoleFormatterOptions.cs | 5 ++++- src/Elastic.Extensions.Logging/ElasticsearchLogger.cs | 2 ++ .../Options/ElasticsearchLoggerOptions.cs | 6 +++++- 7 files changed, 32 insertions(+), 7 deletions(-) diff --git a/examples/Elastic.Extensions.Logging.Console.Example/Elastic.Extensions.Logging.Console.Example.csproj b/examples/Elastic.Extensions.Logging.Console.Example/Elastic.Extensions.Logging.Console.Example.csproj index 05efcc92..4d730f65 100644 --- a/examples/Elastic.Extensions.Logging.Console.Example/Elastic.Extensions.Logging.Console.Example.csproj +++ b/examples/Elastic.Extensions.Logging.Console.Example/Elastic.Extensions.Logging.Console.Example.csproj @@ -2,7 +2,7 @@ Exe - net6.0 + net8.0 enable enable diff --git a/examples/Elastic.Extensions.Logging.Console.Example/Program.cs b/examples/Elastic.Extensions.Logging.Console.Example/Program.cs index e1e08cd5..33f4c6eb 100644 --- a/examples/Elastic.Extensions.Logging.Console.Example/Program.cs +++ b/examples/Elastic.Extensions.Logging.Console.Example/Program.cs @@ -1,11 +1,12 @@ // See https://aka.ms/new-console-template for more information +using Elastic.CommonSchema; using Elastic.Extensions.Logging.Console; using Elastic.Extensions.Logging.Console.Example; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; +using Host = Microsoft.Extensions.Hosting.Host; await Host.CreateDefaultBuilder(args) .UseConsoleLifetime() @@ -13,7 +14,10 @@ await Host.CreateDefaultBuilder(args) { configurationBuilder.SetBasePath(AppDomain.CurrentDomain.BaseDirectory); }) - .ConfigureLogging((_, loggingBuilder) => loggingBuilder.AddEcsConsole()) + .ConfigureLogging((_, loggingBuilder) => loggingBuilder.AddEcsConsole(configure: c => + { + c.MapCustom = l => l.Organization = new Organization { Name = "my-organization" }; + })) .ConfigureServices((_, services) => { services.AddHostedService(); diff --git a/src/Elastic.Extensions.Logging.Common/ILogEventCreationOptions.cs b/src/Elastic.Extensions.Logging.Common/ILogEventCreationOptions.cs index efca528b..40a24bfa 100644 --- a/src/Elastic.Extensions.Logging.Common/ILogEventCreationOptions.cs +++ b/src/Elastic.Extensions.Logging.Common/ILogEventCreationOptions.cs @@ -18,3 +18,13 @@ public interface ILogEventCreationOptions : IEcsDocumentCreationOptions /// string ListSeparator { get; set; } } + +/// +public interface ILogEventCreationOptions : ILogEventCreationOptions + where TEcsDocument : EcsDocument, new() +{ + /// + /// Allows you to enrich using before its being formatted + /// + Action? MapCustom { get; set; } +} diff --git a/src/Elastic.Extensions.Logging.Console/EcsConsoleFormatter.cs b/src/Elastic.Extensions.Logging.Console/EcsConsoleFormatter.cs index e52fd5a5..420b0f43 100644 --- a/src/Elastic.Extensions.Logging.Console/EcsConsoleFormatter.cs +++ b/src/Elastic.Extensions.Logging.Console/EcsConsoleFormatter.cs @@ -34,10 +34,10 @@ public override void Write(in LogEntry logEntry, IExternalScopeP var logLevel = logEntry.LogLevel; var categoryName = logEntry.Category; var eventId = logEntry.EventId; + + logEvent.Message = message; logEvent.Log = new Log { Level = logLevel.ToEcsLogLevelString(), Logger = categoryName }; logEvent.Event = new Event { Action = eventId.Name, Code = eventId.Id.ToString(), Severity = logLevel.ToEcsSeverity() }; - logEvent.Message = message; - logEvent.Agent = DefaultAgent; if (_options.Tags is { Length: > 0 }) logEvent.Tags = _options.Tags; @@ -48,6 +48,8 @@ public override void Write(in LogEntry logEntry, IExternalScopeP // These will overwrite any scope values with the same name logEvent.AddStateValues(logEntry.State, _options); + _options.MapCustom?.Invoke(logEvent); + textWriter.WriteLine(logEvent.Serialize()); } diff --git a/src/Elastic.Extensions.Logging.Console/EcsConsoleFormatterOptions.cs b/src/Elastic.Extensions.Logging.Console/EcsConsoleFormatterOptions.cs index bb37508b..682c1b4e 100644 --- a/src/Elastic.Extensions.Logging.Console/EcsConsoleFormatterOptions.cs +++ b/src/Elastic.Extensions.Logging.Console/EcsConsoleFormatterOptions.cs @@ -6,7 +6,7 @@ namespace Elastic.Extensions.Logging.Console; /// -public class EcsConsoleFormatterOptions : ConsoleFormatterOptions, ILogEventCreationOptions +public class EcsConsoleFormatterOptions : ConsoleFormatterOptions, ILogEventCreationOptions { /// public bool IncludeHost { get; set; } = true; @@ -25,4 +25,7 @@ public class EcsConsoleFormatterOptions : ConsoleFormatterOptions, ILogEventCrea /// public string ListSeparator { get; set; } = ", "; + + /// + public Action? MapCustom { get; set; } } diff --git a/src/Elastic.Extensions.Logging/ElasticsearchLogger.cs b/src/Elastic.Extensions.Logging/ElasticsearchLogger.cs index 1e6290a4..30d1f8ff 100644 --- a/src/Elastic.Extensions.Logging/ElasticsearchLogger.cs +++ b/src/Elastic.Extensions.Logging/ElasticsearchLogger.cs @@ -99,6 +99,8 @@ Func formatter // These will overwrite any scope values with the same name logEvent.AddStateValues(state, _options); + _options.MapCustom?.Invoke(logEvent); + return logEvent; } diff --git a/src/Elastic.Extensions.Logging/Options/ElasticsearchLoggerOptions.cs b/src/Elastic.Extensions.Logging/Options/ElasticsearchLoggerOptions.cs index 75737599..17d441df 100644 --- a/src/Elastic.Extensions.Logging/Options/ElasticsearchLoggerOptions.cs +++ b/src/Elastic.Extensions.Logging/Options/ElasticsearchLoggerOptions.cs @@ -1,3 +1,4 @@ +using System; using Elastic.CommonSchema; using Elastic.Extensions.Logging.Common; using Elastic.Ingest.Elasticsearch; @@ -8,7 +9,7 @@ namespace Elastic.Extensions.Logging.Options /// /// Provide options to to control how data gets written to Elasticsearch /// - public class ElasticsearchLoggerOptions : ILogEventCreationOptions + public class ElasticsearchLoggerOptions : ILogEventCreationOptions { /// /// Gets or sets a flag indicating whether host details should be included in the message. Defaults to true. @@ -91,5 +92,8 @@ public class ElasticsearchLoggerOptions : ILogEventCreationOptions /// If set takes precedence over /// public ITransport? Transport { get; set; } + + /// + public Action? MapCustom { get; set; } } }