Skip to content

Commit

Permalink
added reproduction for akkadotnet#7255
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaronontheweb committed Jun 14, 2024
1 parent 3e7e1d0 commit 2741bef
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions src/core/Akka.API.Tests/LogFormatSpec.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// -----------------------------------------------------------------------
// <copyright file="LogFormatSpec.cs" company="Akka.NET Project">
// Copyright (C) 2009-2024 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2024 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
// -----------------------------------------------------------------------

using System;
using System.IO;
using Akka.Event;
using FluentAssertions;
using VerifyXunit;
using Xunit;
using Xunit.Abstractions;

namespace Akka.API.Tests;

/// <summary>
/// Regression test for https://github.com/akkadotnet/akka.net/issues/7255
///
/// Need to assert that the default log format is still working as expected.
/// </summary>
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);
}
}

0 comments on commit 2741bef

Please sign in to comment.