|
1 | 1 | using System; |
| 2 | +using System.Collections.Generic; |
2 | 3 | using Akka.Actor; |
3 | 4 | using Akka.Configuration; |
4 | 5 | using Akka.Event; |
5 | 6 | using FluentAssertions; |
6 | 7 | using Serilog; |
7 | 8 | using Xunit; |
8 | 9 | using Xunit.Abstractions; |
| 10 | +using SerilogLog = Serilog.Log; |
9 | 11 |
|
10 | 12 | namespace Akka.Logger.Serilog.Tests |
11 | 13 | { |
12 | 14 | public class SerilogFormattingSpecs : TestKit.Xunit2.TestKit |
13 | 15 | { |
14 | | - public static readonly Config Config = @"akka.loglevel = DEBUG"; |
15 | | - private ILogger _serilogLogger; |
| 16 | + public static readonly Config Config = |
| 17 | +@" |
| 18 | +akka.loglevel = DEBUG |
| 19 | +# akka.loggers=[""Akka.Logger.Serilog.SerilogLogger, Akka.Logger.Serilog""] |
| 20 | +"; |
| 21 | + private readonly ILogger _serilogLogger; |
| 22 | + private readonly TestSink _sink; |
16 | 23 |
|
17 | | - private ILoggingAdapter _loggingAdapter; |
| 24 | + private readonly ILoggingAdapter _loggingAdapter; |
18 | 25 |
|
19 | 26 | public SerilogFormattingSpecs(ITestOutputHelper helper) : base(Config, output: helper) |
20 | 27 | { |
| 28 | + _sink = new TestSink(helper); |
| 29 | + |
21 | 30 | _serilogLogger = new LoggerConfiguration() |
22 | 31 | .WriteTo.ColoredConsole() |
| 32 | + .WriteTo.Sink(_sink) |
23 | 33 | .MinimumLevel.Information() |
24 | 34 | .CreateLogger(); |
| 35 | + |
| 36 | + SerilogLog.Logger = _serilogLogger; |
25 | 37 |
|
26 | 38 | var logSource = Sys.Name; |
27 | 39 | var logClass = typeof(ActorSystem); |
28 | 40 |
|
29 | 41 | _loggingAdapter = new SerilogLoggingAdapter(Sys.EventStream, logSource, logClass); |
30 | 42 | } |
31 | 43 |
|
| 44 | + [Fact] |
| 45 | + public void LogOutputRegressionTest() |
| 46 | + { |
| 47 | + const string message = "{IntArray} {DoubleArray} {StringArray} {DoubleList}"; |
| 48 | + const string expectedMessage = "[0, 1, 2] [0.1, 0.2, 0.3] [\"One\", \"Two\"] [1, 2, 3]"; |
| 49 | + var args = new object[] |
| 50 | + { |
| 51 | + new int[] { 0, 1, 2 }, |
| 52 | + new double[] { 0.1, 0.2, 0.3 }, |
| 53 | + new string[] { "One", "Two" }, |
| 54 | + new List<double> { 1, 2, 3 } |
| 55 | + }; |
| 56 | + |
| 57 | + _sink.Clear(); |
| 58 | + AwaitCondition(() => _sink.Writes.Count == 0); |
| 59 | + |
| 60 | + _serilogLogger.Information(message, args); |
| 61 | + AwaitCondition(() => _sink.Writes.Count == 1); |
| 62 | + |
| 63 | + _sink.Writes.TryDequeue(out var logEvent).Should().BeTrue(); |
| 64 | + logEvent.RenderMessage().Should().Be(expectedMessage); |
| 65 | + |
| 66 | + Sys.EventStream.Subscribe(TestActor, typeof(LogEvent)); |
| 67 | + _loggingAdapter.Log(LogLevel.InfoLevel, message, args); |
| 68 | + var akkaLogEvent = ExpectMsg<LogEvent>(); |
| 69 | + |
| 70 | + akkaLogEvent.ToString().Should().Contain(expectedMessage); |
| 71 | + } |
| 72 | + |
32 | 73 | [Theory] |
33 | 74 | [InlineData(LogLevel.DebugLevel, "test case {0}", new object[]{ 1 })] |
34 | 75 | [InlineData(LogLevel.DebugLevel, "test case {myNum}", new object[] { 1 })] |
|
0 commit comments