From 2741bef7c6edc73124a8b993e368e76c2e7babe6 Mon Sep 17 00:00:00 2001 From: Aaron Stannard Date: Thu, 13 Jun 2024 19:55:28 -0500 Subject: [PATCH] added reproduction for #7255 --- src/core/Akka.API.Tests/LogFormatSpec.cs | 81 ++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 src/core/Akka.API.Tests/LogFormatSpec.cs diff --git a/src/core/Akka.API.Tests/LogFormatSpec.cs b/src/core/Akka.API.Tests/LogFormatSpec.cs new file mode 100644 index 00000000000..d1ac3fce06d --- /dev/null +++ b/src/core/Akka.API.Tests/LogFormatSpec.cs @@ -0,0 +1,81 @@ +// ----------------------------------------------------------------------- +// +// Copyright (C) 2009-2024 Lightbend Inc. +// Copyright (C) 2013-2024 .NET Foundation +// +// ----------------------------------------------------------------------- + +using System; +using System.IO; +using Akka.Event; +using FluentAssertions; +using VerifyXunit; +using Xunit; +using Xunit.Abstractions; + +namespace Akka.API.Tests; + +/// +/// Regression test for https://github.com/akkadotnet/akka.net/issues/7255 +/// +/// Need to assert that the default log format is still working as expected. +/// +public sealed class DefaultLogFormatSpec : TestKit.Xunit2.TestKit +{ + public DefaultLogFormatSpec() : base("akka.loglevel = DEBUG") + { + } + + public class OutputRedirector : IDisposable + { + private readonly TextWriter _originalOutput; + private readonly StreamWriter _writer; + + public OutputRedirector(string filePath) + { + _originalOutput = Console.Out; + _writer = new StreamWriter(filePath) + { + AutoFlush = true + }; + Console.SetOut(_writer); + } + + public void Dispose() + { + Console.SetOut(_originalOutput); + _writer.Dispose(); + } + } + + [Fact] + public void ShouldUseDefaultLogFormat() + { + // arrange + var filePath = Path.GetTempFileName(); + + // act + using (new OutputRedirector(filePath)) + { + Sys.Log.Debug("This is a test {0} {1}", 1, "cheese"); + Sys.Log.Info("This is a test {0}", 1); + Sys.Log.Warning("This is a test {0}", 1); + Sys.Log.Error("This is a test {0}", 1); + + try + { + throw new Exception("boom!"); + } + catch (Exception ex) + { + Sys.Log.Debug(ex, "This is a test {0} {1}", 1, "cheese"); + Sys.Log.Info(ex, "This is a test {0}", 1); + Sys.Log.Warning(ex, "This is a test {0}", 1); + Sys.Log.Error(ex, "This is a test {0}", 1); + } + } + + // assert + Verifier.VerifyFile(filePath); + } +} \ No newline at end of file