From 508cc78239b8a7391c5f8e0a4f1f94171bd8c7e9 Mon Sep 17 00:00:00 2001 From: Aaron Stannard Date: Wed, 17 Jul 2024 16:15:38 -0500 Subject: [PATCH 1/8] added LogScope to all MSFT.EXT.Logger output --- src/Akka.Hosting/Logging/LoggerFactoryLogger.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Akka.Hosting/Logging/LoggerFactoryLogger.cs b/src/Akka.Hosting/Logging/LoggerFactoryLogger.cs index 95d2bb11..41397411 100644 --- a/src/Akka.Hosting/Logging/LoggerFactoryLogger.cs +++ b/src/Akka.Hosting/Logging/LoggerFactoryLogger.cs @@ -62,7 +62,16 @@ protected override bool Receive(object message) protected virtual void Log(LogEvent log, ActorPath path) { - _akkaLogger.Log(GetLogLevel(log.LogLevel()), new EventId(), log, log.Cause, (@event, exception) => @event.ToString()); + using (_akkaLogger.BeginScope(new Dictionary + { + ["LogSource"] = log.LogSource, + ["LogClass"] = log.LogClass, + ["LogSourceThread"] = log.Thread.ManagedThreadId, + })) + { + _akkaLogger.Log(GetLogLevel(log.LogLevel()), new EventId(), log, log.Cause, (@event, exception) => @event.ToString()); + } + } private static LogLevel GetLogLevel(Event.LogLevel level) From ddd5c1f0ed44d3ba4c7adebf46b050eef97d30f8 Mon Sep 17 00:00:00 2001 From: Aaron Stannard Date: Wed, 17 Jul 2024 16:20:52 -0500 Subject: [PATCH 2/8] added a null scoping operator --- src/Akka.Hosting.Tests/XUnitLogger.cs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Akka.Hosting.Tests/XUnitLogger.cs b/src/Akka.Hosting.Tests/XUnitLogger.cs index f7283aed..2dbfb036 100644 --- a/src/Akka.Hosting.Tests/XUnitLogger.cs +++ b/src/Akka.Hosting.Tests/XUnitLogger.cs @@ -6,6 +6,20 @@ namespace Akka.Hosting.Tests; public class XUnitLogger: ILogger { + internal sealed class NullScope : IDisposable + { + public static NullScope Instance { get; } = new NullScope(); + + private NullScope() + { + } + + /// + public void Dispose() + { + } + } + private const string NullFormatted = "[null]"; private readonly string _category; @@ -60,7 +74,7 @@ public bool IsEnabled(LogLevel logLevel) public IDisposable BeginScope(TState state) { - throw new NotImplementedException(); + return NullScope.Instance; } private static bool TryFormatMessage( From f0092980bc8cf19123a967ae697f932ac8d0d329 Mon Sep 17 00:00:00 2001 From: Aaron Stannard Date: Wed, 17 Jul 2024 16:31:25 -0500 Subject: [PATCH 3/8] cleaning up `XunitLogger` instances --- RELEASE_NOTES.md | 5 +- .../Internals/XUnitLogger.cs | 17 +++- .../Akka.Hosting.Tests.csproj | 6 ++ src/Akka.Hosting.Tests/ExtensionsSpecs.cs | 3 +- .../Logging/LogMessageFormatterSpec.cs | 1 + src/Akka.Hosting.Tests/XUnitLogger.cs | 98 ------------------- src/Akka.Hosting.Tests/XUnitLoggerProvider.cs | 26 ----- src/Directory.Build.props | 5 +- 8 files changed, 30 insertions(+), 131 deletions(-) delete mode 100644 src/Akka.Hosting.Tests/XUnitLogger.cs delete mode 100644 src/Akka.Hosting.Tests/XUnitLoggerProvider.cs diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index bb61c33d..f876d958 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -1,6 +1,7 @@ -## [1.5.25] / 17 June 2024 +## [1.5.26] / 17 July 2024 -* [Update Akka.NET to 1.5.25](https://github.com/akkadotnet/akka.net/releases/tag/1.5.25), which resolves a critical bug: [Akka.Logging: v1.5.21 appears to have truncated log source, timestamps, etc from all log messages ](https://github.com/akkadotnet/akka.net/issues/7255) +* [Update Akka.NET to 1.5.26](https://github.com/akkadotnet/akka.net/releases/tag/1.5.26) +* Added scopes to all Microsoft.Extensions.Logging output, for better semantic logging support. ## [1.5.24] / 10 June 2024 diff --git a/src/Akka.Hosting.TestKit/Internals/XUnitLogger.cs b/src/Akka.Hosting.TestKit/Internals/XUnitLogger.cs index 3d47f4e6..476511e5 100644 --- a/src/Akka.Hosting.TestKit/Internals/XUnitLogger.cs +++ b/src/Akka.Hosting.TestKit/Internals/XUnitLogger.cs @@ -7,6 +7,21 @@ namespace Akka.Hosting.TestKit.Internals public class XUnitLogger: ILogger { private const string NullFormatted = "[null]"; + + internal sealed class NullScope : IDisposable + { + public static NullScope Instance { get; } = new NullScope(); + + private NullScope() + { + } + + /// + public void Dispose() + { + } + } + private readonly string _category; private readonly ITestOutputHelper _helper; @@ -60,7 +75,7 @@ public bool IsEnabled(LogLevel logLevel) public IDisposable BeginScope(TState state) where TState : notnull { - throw new NotImplementedException(); + return NullScope.Instance; } private static bool TryFormatMessage( diff --git a/src/Akka.Hosting.Tests/Akka.Hosting.Tests.csproj b/src/Akka.Hosting.Tests/Akka.Hosting.Tests.csproj index 4651277f..353d3976 100644 --- a/src/Akka.Hosting.Tests/Akka.Hosting.Tests.csproj +++ b/src/Akka.Hosting.Tests/Akka.Hosting.Tests.csproj @@ -31,4 +31,10 @@ Always + + + + bin\Debug\net6.0\Akka.Hosting.TestKit.dll + + diff --git a/src/Akka.Hosting.Tests/ExtensionsSpecs.cs b/src/Akka.Hosting.Tests/ExtensionsSpecs.cs index 9005f701..0c4050e4 100644 --- a/src/Akka.Hosting.Tests/ExtensionsSpecs.cs +++ b/src/Akka.Hosting.Tests/ExtensionsSpecs.cs @@ -8,8 +8,7 @@ using System.Threading.Tasks; using Akka.Actor; using Akka.Configuration; -using Akka.Event; -using Akka.TestKit.Xunit2.Internals; +using Akka.Hosting.TestKit.Internals; using FluentAssertions; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; diff --git a/src/Akka.Hosting.Tests/Logging/LogMessageFormatterSpec.cs b/src/Akka.Hosting.Tests/Logging/LogMessageFormatterSpec.cs index dfd2adb7..94fe72f9 100644 --- a/src/Akka.Hosting.Tests/Logging/LogMessageFormatterSpec.cs +++ b/src/Akka.Hosting.Tests/Logging/LogMessageFormatterSpec.cs @@ -12,6 +12,7 @@ using Akka.Actor; using Akka.Configuration; using Akka.Event; +using Akka.Hosting.TestKit.Internals; using FluentAssertions; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; diff --git a/src/Akka.Hosting.Tests/XUnitLogger.cs b/src/Akka.Hosting.Tests/XUnitLogger.cs deleted file mode 100644 index 2dbfb036..00000000 --- a/src/Akka.Hosting.Tests/XUnitLogger.cs +++ /dev/null @@ -1,98 +0,0 @@ -using System; -using Microsoft.Extensions.Logging; -using Xunit.Abstractions; - -namespace Akka.Hosting.Tests; - -public class XUnitLogger: ILogger -{ - internal sealed class NullScope : IDisposable - { - public static NullScope Instance { get; } = new NullScope(); - - private NullScope() - { - } - - /// - public void Dispose() - { - } - } - - private const string NullFormatted = "[null]"; - - private readonly string _category; - private readonly ITestOutputHelper _helper; - private readonly LogLevel _logLevel; - - public XUnitLogger(string category, ITestOutputHelper helper, LogLevel logLevel) - { - _category = category; - _helper = helper; - _logLevel = logLevel; - } - - public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter) - { - if (!IsEnabled(logLevel)) - return; - - if (!TryFormatMessage(state, exception, formatter, out var formattedMessage)) - return; - - WriteLogEntry(logLevel, eventId, formattedMessage, exception); - } - - private void WriteLogEntry(LogLevel logLevel, EventId eventId, string? message, Exception? exception) - { - var level = logLevel switch - { - LogLevel.Critical => "CRT", - LogLevel.Debug => "DBG", - LogLevel.Error => "ERR", - LogLevel.Information => "INF", - LogLevel.Warning => "WRN", - LogLevel.Trace => "DBG", - _ => "???" - }; - - var msg = $"{DateTime.Now}:{level}:{_category}:{eventId} {message}"; - if (exception != null) - msg += $"\n{exception.GetType()} {exception.Message}\n{exception.StackTrace}"; - _helper.WriteLine(msg); - } - - public bool IsEnabled(LogLevel logLevel) - { - return logLevel switch - { - LogLevel.None => false, - _ => logLevel >= _logLevel - }; - } - - public IDisposable BeginScope(TState state) - { - return NullScope.Instance; - } - - private static bool TryFormatMessage( - TState state, - Exception? exception, - Func formatter, - out string? result) - { - formatter = formatter ?? throw new ArgumentNullException(nameof(formatter)); - - var formattedMessage = formatter(state, exception); - if (formattedMessage == NullFormatted) - { - result = null; - return false; - } - - result = formattedMessage; - return true; - } -} \ No newline at end of file diff --git a/src/Akka.Hosting.Tests/XUnitLoggerProvider.cs b/src/Akka.Hosting.Tests/XUnitLoggerProvider.cs deleted file mode 100644 index c5f8d18c..00000000 --- a/src/Akka.Hosting.Tests/XUnitLoggerProvider.cs +++ /dev/null @@ -1,26 +0,0 @@ -using Microsoft.Extensions.Logging; -using Xunit.Abstractions; - -namespace Akka.Hosting.Tests; - -public class XUnitLoggerProvider : ILoggerProvider -{ - private readonly ITestOutputHelper _helper; - private readonly LogLevel _logLevel; - - public XUnitLoggerProvider(ITestOutputHelper helper, LogLevel logLevel) - { - _helper = helper; - _logLevel = logLevel; - } - - public void Dispose() - { - // no-op - } - - public ILogger CreateLogger(string categoryName) - { - return new XUnitLogger(categoryName, _helper, _logLevel); - } -} \ No newline at end of file diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 1b450d8c..1fbc792c 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -2,8 +2,9 @@ Copyright © 2013-2023 Akka.NET Team Akka.NET Team - 1.5.19 - • [Update Akka.NET to 1.5.19](https://github.com/akkadotnet/akka.net/releases/tag/1.5.19) + 1.5.26 + • [Update Akka.NET to 1.5.26](https://github.com/akkadotnet/akka.net/releases/tag/1.5.26) +• Added scopes to all Microsoft.Extensions.Logging output%2C for better semantic logging support. akkalogo.png https://github.com/akkadotnet/Akka.Hosting From 0cf84fa55be4cb659fb4b5fb388777dd90413c7f Mon Sep 17 00:00:00 2001 From: Aaron Stannard Date: Wed, 17 Jul 2024 16:40:05 -0500 Subject: [PATCH 4/8] fixed warning --- src/Akka.Hosting.TestKit/Internals/XUnitLogger.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Akka.Hosting.TestKit/Internals/XUnitLogger.cs b/src/Akka.Hosting.TestKit/Internals/XUnitLogger.cs index 476511e5..1cbe5f3b 100644 --- a/src/Akka.Hosting.TestKit/Internals/XUnitLogger.cs +++ b/src/Akka.Hosting.TestKit/Internals/XUnitLogger.cs @@ -73,7 +73,7 @@ public bool IsEnabled(LogLevel logLevel) }; } - public IDisposable BeginScope(TState state) where TState : notnull + public IDisposable BeginScope(TState state) { return NullScope.Instance; } From dea35500791db4abb98945f4ef8fafadad84a83c Mon Sep 17 00:00:00 2001 From: Aaron Stannard Date: Wed, 17 Jul 2024 16:49:46 -0500 Subject: [PATCH 5/8] Revert "fixed warning" This reverts commit 0cf84fa55be4cb659fb4b5fb388777dd90413c7f. --- src/Akka.Hosting.TestKit/Internals/XUnitLogger.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Akka.Hosting.TestKit/Internals/XUnitLogger.cs b/src/Akka.Hosting.TestKit/Internals/XUnitLogger.cs index 1cbe5f3b..476511e5 100644 --- a/src/Akka.Hosting.TestKit/Internals/XUnitLogger.cs +++ b/src/Akka.Hosting.TestKit/Internals/XUnitLogger.cs @@ -73,7 +73,7 @@ public bool IsEnabled(LogLevel logLevel) }; } - public IDisposable BeginScope(TState state) + public IDisposable BeginScope(TState state) where TState : notnull { return NullScope.Instance; } From 65a03343d28984624c63e9174ddc7b9c4db7b17c Mon Sep 17 00:00:00 2001 From: Aaron Stannard Date: Wed, 17 Jul 2024 16:51:38 -0500 Subject: [PATCH 6/8] Reapply "fixed warning" This reverts commit dea35500791db4abb98945f4ef8fafadad84a83c. --- src/Akka.Hosting.TestKit/Internals/XUnitLogger.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Akka.Hosting.TestKit/Internals/XUnitLogger.cs b/src/Akka.Hosting.TestKit/Internals/XUnitLogger.cs index 476511e5..1cbe5f3b 100644 --- a/src/Akka.Hosting.TestKit/Internals/XUnitLogger.cs +++ b/src/Akka.Hosting.TestKit/Internals/XUnitLogger.cs @@ -73,7 +73,7 @@ public bool IsEnabled(LogLevel logLevel) }; } - public IDisposable BeginScope(TState state) where TState : notnull + public IDisposable BeginScope(TState state) { return NullScope.Instance; } From a3b7f6e68495817d8ac90ee953a6cf9d103f6d66 Mon Sep 17 00:00:00 2001 From: Aaron Stannard Date: Wed, 17 Jul 2024 16:51:48 -0500 Subject: [PATCH 7/8] added API approvals --- .../verify/CoreApiSpec.ApproveTestKit.verified.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Akka.Hosting.API.Tests/verify/CoreApiSpec.ApproveTestKit.verified.txt b/src/Akka.Hosting.API.Tests/verify/CoreApiSpec.ApproveTestKit.verified.txt index 6904b52c..98065745 100644 --- a/src/Akka.Hosting.API.Tests/verify/CoreApiSpec.ApproveTestKit.verified.txt +++ b/src/Akka.Hosting.API.Tests/verify/CoreApiSpec.ApproveTestKit.verified.txt @@ -9,8 +9,7 @@ namespace Akka.Hosting.TestKit.Internals public class XUnitLogger : Microsoft.Extensions.Logging.ILogger { public XUnitLogger(string category, Xunit.Abstractions.ITestOutputHelper helper, Microsoft.Extensions.Logging.LogLevel logLevel) { } - public System.IDisposable BeginScope(TState state) - where TState : notnull { } + public System.IDisposable BeginScope(TState state) { } public bool IsEnabled(Microsoft.Extensions.Logging.LogLevel logLevel) { } public void Log(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, TState state, System.Exception? exception, System.Func formatter) { } } From 945e2d5119c82713f57153de95994b08b308da27 Mon Sep 17 00:00:00 2001 From: Aaron Stannard Date: Wed, 17 Jul 2024 16:56:38 -0500 Subject: [PATCH 8/8] cleaned up the format string --- src/Akka.Hosting/Logging/LoggerFactoryLogger.cs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/Akka.Hosting/Logging/LoggerFactoryLogger.cs b/src/Akka.Hosting/Logging/LoggerFactoryLogger.cs index 41397411..2e197dcf 100644 --- a/src/Akka.Hosting/Logging/LoggerFactoryLogger.cs +++ b/src/Akka.Hosting/Logging/LoggerFactoryLogger.cs @@ -62,12 +62,7 @@ protected override bool Receive(object message) protected virtual void Log(LogEvent log, ActorPath path) { - using (_akkaLogger.BeginScope(new Dictionary - { - ["LogSource"] = log.LogSource, - ["LogClass"] = log.LogClass, - ["LogSourceThread"] = log.Thread.ManagedThreadId, - })) + using (_akkaLogger.BeginScope("{AkkaLogSource} {AkkaLogClass}", log.LogSource, log.LogClass)) { _akkaLogger.Log(GetLogLevel(log.LogLevel()), new EventId(), log, log.Cause, (@event, exception) => @event.ToString()); }