-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Akka.Event: restore missing log data #7256
Changes from all commits
2741bef
5562238
a250cc9
f31e9e6
ed0ebba
585668c
0a41a38
098a265
c190a4f
c8c40fa
a3bcb43
668d176
1e3cfc7
f18eced
50046d4
0dc42fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,7 +55,7 @@ let runIncrementally = hasBuildParam "incremental" | |
let incrementalistReport = output @@ "incrementalist.txt" | ||
|
||
// Configuration values for tests | ||
let testNetFrameworkVersion = "net471" | ||
let testNetFrameworkVersion = "net48" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Upgraded to .NET 4.8 so we could use newer versions of Verify, which had |
||
let testNetVersion = "net8.0" | ||
|
||
Target "Clean" (fun _ -> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,9 +5,6 @@ | |
// </copyright> | ||
//----------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.IO; | ||
using System.Reflection; | ||
using System.Threading.Tasks; | ||
using Akka.Actor; | ||
using Akka.Cluster; | ||
|
@@ -28,7 +25,6 @@ | |
|
||
namespace Akka.API.Tests | ||
{ | ||
[UsesVerify] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Obsolete in the latest version of Verify |
||
public class CoreAPISpec | ||
{ | ||
static Task VerifyAssembly<T>() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// ----------------------------------------------------------------------- | ||
// <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.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
using System.Threading.Tasks; | ||
using Akka.Actor; | ||
using Akka.Actor.Setup; | ||
using Akka.Configuration; | ||
using Akka.Event; | ||
using VerifyXunit; | ||
using Xunit; | ||
|
||
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(CustomLoggerSetup()) | ||
{ | ||
_logger = (CustomLogger)Sys.Settings.StdoutLogger; | ||
} | ||
|
||
private readonly CustomLogger _logger; | ||
|
||
public class CustomLogger : StandardOutLogger | ||
{ | ||
protected override void Log(object message) | ||
{ | ||
base.Log(message); // log first, just so we can be sure it's hit STDOUT | ||
if (message is LogEvent e) | ||
{ | ||
_events.Add(e); | ||
} | ||
|
||
} | ||
|
||
private readonly ConcurrentBag<LogEvent> _events = new(); | ||
public IReadOnlyCollection<LogEvent> Events => _events; | ||
} | ||
|
||
public static ActorSystemSetup CustomLoggerSetup() | ||
{ | ||
var hocon = @$" | ||
akka.loglevel = DEBUG | ||
akka.stdout-logger-class = ""{typeof(CustomLogger).AssemblyQualifiedName}"""; | ||
var bootstrapSetup = BootstrapSetup.Create().WithConfig(ConfigurationFactory.ParseString(hocon)); | ||
return ActorSystemSetup.Create(bootstrapSetup); | ||
} | ||
|
||
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 async Task ShouldUseDefaultLogFormat() | ||
{ | ||
// arrange | ||
var filePath = Path.GetTempFileName(); | ||
|
||
// act | ||
using (new OutputRedirector(filePath)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The error in #7255 was caused not by the formatter on the message changing, but by the fact that we started calling |
||
{ | ||
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); | ||
|
||
// force all logs to be received | ||
await AwaitConditionAsync(() => | ||
{ | ||
return _logger.Events.Count(c => c.Message.ToString()!.Contains("This is a test")) == 4; | ||
}); | ||
} | ||
|
||
// assert | ||
// ReSharper disable once MethodHasAsyncOverload | ||
var text = File.ReadAllText(filePath); | ||
|
||
// need to sanitize the thread id | ||
text = SanitizeDateTime(text); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Scrub thread ids and datetimes, since these are unstable run-per-run even if everything is working normally. |
||
text = SanitizeThreadNumber(text); | ||
|
||
await Verifier.Verify(text); | ||
} | ||
|
||
static string SanitizeThreadNumber(string log) | ||
{ | ||
string pattern = @"(\[Thread )\d+(\])"; | ||
string replacement = "[Thread 0001]"; | ||
string result = Regex.Replace(log, pattern, replacement); | ||
return result; | ||
} | ||
|
||
static string SanitizeDateTime(string logs, string replacement = "DateTime") | ||
{ | ||
// Regular expression to match the datetime | ||
string pattern = @"\[\d{2}/\d{2}/\d{4} \d{2}:\d{2}:\d{2}\.\d{3}Z\]"; | ||
|
||
// Replace all occurrences of the datetime with the constant value | ||
string result = Regex.Replace(logs, pattern, $"[{replacement}]", RegexOptions.Multiline); | ||
|
||
return result; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ public static class ModuleInit | |
[ModuleInitializer] | ||
public static void Init() | ||
{ | ||
VerifyDiffPlex.Initialize(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Force DiffPlex to start once at boot. |
||
VerifierSettings.ScrubLinesContaining("[assembly: ReleaseDateAttribute("); | ||
Verifier.UseProjectRelativeDirectory("verify"); | ||
VerifierSettings.UniqueForRuntime(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[DEBUG][DateTime][Thread 0001][ActorSystem(test)] This is a test 1 cheese | ||
[INFO][DateTime][Thread 0001][ActorSystem(test)] This is a test 1 | ||
[WARNING][DateTime][Thread 0001][ActorSystem(test)] This is a test 1 | ||
[ERROR][DateTime][Thread 0001][ActorSystem(test)] This is a test 1 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[DEBUG][DateTime][Thread 0001][ActorSystem(test)] This is a test 1 cheese | ||
[INFO][DateTime][Thread 0001][ActorSystem(test)] This is a test 1 | ||
[WARNING][DateTime][Thread 0001][ActorSystem(test)] This is a test 1 | ||
[ERROR][DateTime][Thread 0001][ActorSystem(test)] This is a test 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No idea why the stack trace logs don't show up on .NET Framework, but they do on .NET 8 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
|
||
namespace DocsExamples.Streams | ||
{ | ||
#if NETCOREAPP | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Caused a grumble on Linux, which caused DocFx validation to fail |
||
public class RestartDocTests : TestKit | ||
{ | ||
private ActorMaterializer Materializer { get; } | ||
|
@@ -65,4 +66,5 @@ public void Restart_stages_should_demonstrate_a_restart_with_backoff_source() | |
#endregion | ||
} | ||
} | ||
#endif | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -191,7 +191,7 @@ public virtual bool ShouldTryKeepMessage(LogEvent evt, out string expandedLogMes | |
else | ||
{ | ||
// allocate the message just once | ||
var nullCheck = evt.Message.ToString(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are the fixes here - we were only looking at the message, not the entire log string. |
||
var nullCheck = evt.ToString(); | ||
|
||
if (nullCheck == null) | ||
return false; // no message to filter | ||
|
@@ -225,7 +225,7 @@ private EmptyLogFilterEvaluator() : base(Array.Empty<LogFilterBase>()) | |
|
||
public override bool ShouldTryKeepMessage(LogEvent evt, out string expandedLogMessage) | ||
{ | ||
expandedLogMessage = evt.Message.ToString()!; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the fix for most use cases, when users don't have any log filtering enabled. |
||
expandedLogMessage = evt.ToString()!; | ||
return true; | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add
.received
files from Verify as a build output whenever we have a test failure.