From d2c62f292f7cef81ed3c975648be7ae047fa030c Mon Sep 17 00:00:00 2001 From: Gregorius Soedharmo Date: Fri, 12 May 2023 03:23:58 +0700 Subject: [PATCH 1/2] Add log event property enricher support --- src/Akka.Logger.Serilog/SerilogLogger.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Akka.Logger.Serilog/SerilogLogger.cs b/src/Akka.Logger.Serilog/SerilogLogger.cs index e5d3006..4b8c2d6 100644 --- a/src/Akka.Logger.Serilog/SerilogLogger.cs +++ b/src/Akka.Logger.Serilog/SerilogLogger.cs @@ -51,6 +51,11 @@ private static ILogger GetLogger(LogEvent logEvent) { logger = logMessage.Enrichers.OfType().Aggregate(logger, (current, enricher) => current.ForContext(enricher)); } + if (logEvent.Message is LogMessage message) + { + logger = message.Parameters().Where(a => a is ILogEventEnricher).Aggregate(logger, (current, enricher) => current.ForContext((ILogEventEnricher)enricher)); + } + return logger; } From 93b3ee81377b851b88a1cbe1115ad994170b2dee Mon Sep 17 00:00:00 2001 From: Gregorius Soedharmo Date: Sat, 13 May 2023 00:32:17 +0700 Subject: [PATCH 2/2] Add unit tests --- .../LogMessageSpecs.cs | 26 ++++++++ .../PropertyEnricherSpec.cs | 66 +++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 src/Akka.Logger.Serilog.Tests/PropertyEnricherSpec.cs diff --git a/src/Akka.Logger.Serilog.Tests/LogMessageSpecs.cs b/src/Akka.Logger.Serilog.Tests/LogMessageSpecs.cs index 13904cd..f041d5e 100644 --- a/src/Akka.Logger.Serilog.Tests/LogMessageSpecs.cs +++ b/src/Akka.Logger.Serilog.Tests/LogMessageSpecs.cs @@ -4,6 +4,7 @@ using Akka.Event; using FluentAssertions; using Serilog; +using Serilog.Core.Enrichers; using Serilog.Events; using Xunit; using Xunit.Abstractions; @@ -43,6 +44,31 @@ public void ShouldLogDebugLevelMessage() logEvent.RenderMessage().Should().Contain("hi"); } + [Fact] + public void ShouldLogMessageWithPropertyEnrichers() + { + var context = _loggingAdapter; + + _sink.Clear(); + AwaitCondition(() => _sink.Writes.Count == 0); + + context.Debug("Hi {0}", "Harry Potter", + new PropertyEnricher("Address", "No. 4 Privet Drive"), + new PropertyEnricher("Town", "Little Whinging"), + new PropertyEnricher("County", "Surrey"), + new PropertyEnricher("Country", "England")); + AwaitCondition(() => _sink.Writes.Count == 1); + + _sink.Writes.TryDequeue(out var logEvent).Should().BeTrue(); + logEvent.Level.Should().Be(LogEventLevel.Debug); + logEvent.RenderMessage().Should().Contain("Hi \"Harry Potter\""); + logEvent.Properties.Should().ContainKeys("Address", "Town", "County", "Country"); + logEvent.Properties["Address"].ToString().Should().Be("\"No. 4 Privet Drive\""); + logEvent.Properties["Town"].ToString().Should().Be("\"Little Whinging\""); + logEvent.Properties["County"].ToString().Should().Be("\"Surrey\""); + logEvent.Properties["Country"].ToString().Should().Be("\"England\""); + } + [Fact] public void ShouldLogDebugLevelMessageWithArgs() { diff --git a/src/Akka.Logger.Serilog.Tests/PropertyEnricherSpec.cs b/src/Akka.Logger.Serilog.Tests/PropertyEnricherSpec.cs new file mode 100644 index 0000000..3d09901 --- /dev/null +++ b/src/Akka.Logger.Serilog.Tests/PropertyEnricherSpec.cs @@ -0,0 +1,66 @@ +// ----------------------------------------------------------------------- +// +// Copyright (C) 2009-2023 Lightbend Inc. +// Copyright (C) 2013-2023 .NET Foundation +// +// ----------------------------------------------------------------------- + +using System; +using Akka.Configuration; +using Akka.Event; +using FluentAssertions; +using Serilog; +using Serilog.Core.Enrichers; +using Serilog.Events; +using Xunit; +using Xunit.Abstractions; + +namespace Akka.Logger.Serilog.Tests; + +public class PropertyEnricherSpec : TestKit.Xunit2.TestKit +{ + public static readonly Config Config = $@" +akka.loglevel = DEBUG +akka.loggers = [""{typeof(SerilogLogger).AssemblyQualifiedName}""] +akka.logger-formatter = ""{typeof(SerilogLogMessageFormatter).AssemblyQualifiedName}"" +"; + + private readonly ILoggingAdapter _loggingAdapter; + private readonly TestSink _sink = new TestSink(); + + public PropertyEnricherSpec(ITestOutputHelper helper) : base(Config, output: helper) + { + global::Serilog.Log.Logger = new LoggerConfiguration() + .WriteTo.Sink(_sink) + .MinimumLevel.Debug() + .CreateLogger(); + _loggingAdapter = Sys.Log; + } + + [Fact] + public void ShouldLogMessageWithPropertyEnrichers() + { + var context = _loggingAdapter; + + _sink.Clear(); + AwaitCondition(() => _sink.Writes.Count == 0); + + context.Debug("Hi {Person}", "Harry Potter", + new PropertyEnricher("Address", "No. 4 Privet Drive"), + new PropertyEnricher("Town", "Little Whinging"), + new PropertyEnricher("County", "Surrey"), + new PropertyEnricher("Country", "England")); + AwaitCondition(() => _sink.Writes.Count == 1); + + _sink.Writes.TryDequeue(out var logEvent).Should().BeTrue(); + logEvent.Level.Should().Be(LogEventLevel.Debug); + logEvent.RenderMessage().Should().Contain("Hi \"Harry Potter\""); + logEvent.Properties.Should().ContainKeys("Person", "Address", "Town", "County", "Country"); + logEvent.Properties["Person"].ToString().Should().Be("\"Harry Potter\""); + logEvent.Properties["Address"].ToString().Should().Be("\"No. 4 Privet Drive\""); + logEvent.Properties["Town"].ToString().Should().Be("\"Little Whinging\""); + logEvent.Properties["County"].ToString().Should().Be("\"Surrey\""); + logEvent.Properties["Country"].ToString().Should().Be("\"England\""); + } + +} \ No newline at end of file