Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/Akka.Logger.Serilog.Tests/LogMessageSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Akka.Event;
using FluentAssertions;
using Serilog;
using Serilog.Core.Enrichers;
using Serilog.Events;
using Xunit;
using Xunit.Abstractions;
Expand Down Expand Up @@ -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()
{
Expand Down
66 changes: 66 additions & 0 deletions src/Akka.Logger.Serilog.Tests/PropertyEnricherSpec.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// -----------------------------------------------------------------------
// <copyright file="PropertyEnricherSpec.cs" company="Akka.NET Project">
// Copyright (C) 2009-2023 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2023 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
// -----------------------------------------------------------------------

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\"");
}

}
5 changes: 5 additions & 0 deletions src/Akka.Logger.Serilog/SerilogLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ private static ILogger GetLogger(LogEvent logEvent) {
logger = logMessage.Enrichers.OfType<PropertyEnricher>().Aggregate(logger, (current, enricher) => current.ForContext(enricher));
}

if (logEvent.Message is LogMessage message)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but I'd like to see a unit test added to cover this

{
logger = message.Parameters().Where(a => a is ILogEventEnricher).Aggregate(logger, (current, enricher) => current.ForContext((ILogEventEnricher)enricher));
}

return logger;
}

Expand Down