From 00a06e3af9f6ea2f901720e73c1d11e075f731aa Mon Sep 17 00:00:00 2001 From: Alexey Zimarev Date: Fri, 27 Dec 2024 13:08:16 +0100 Subject: [PATCH] Reduce logging in tests --- .../Fixtures/StoreFixtureBase.cs | 3 -- .../Fixtures/SubscriptionFixtureBase.cs | 2 +- .../SequenceTests.cs | 2 +- .../TestContext.cs | 2 +- .../ProducerTracesTests.cs | 4 +-- .../Fixtures/LegacySubscriptionFixture.cs | 2 +- .../Logging/LoggingExtensions.cs | 33 +++++++++++-------- .../Logging/TUnitLogger.cs | 13 ++++---- 8 files changed, 33 insertions(+), 28 deletions(-) diff --git a/src/Core/test/Eventuous.Tests.Persistence.Base/Fixtures/StoreFixtureBase.cs b/src/Core/test/Eventuous.Tests.Persistence.Base/Fixtures/StoreFixtureBase.cs index e8d88154..32851bf9 100644 --- a/src/Core/test/Eventuous.Tests.Persistence.Base/Fixtures/StoreFixtureBase.cs +++ b/src/Core/test/Eventuous.Tests.Persistence.Base/Fixtures/StoreFixtureBase.cs @@ -2,7 +2,6 @@ using Bogus; using DotNet.Testcontainers.Containers; using Eventuous.TestHelpers; -using Eventuous.TestHelpers.TUnit.Logging; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using TUnit.Core.Interfaces; @@ -26,8 +25,6 @@ public virtual async Task InitializeAsync() { var services = new ServiceCollection(); - services.AddLogging(cfg => cfg.ForTests()); - Serializer = new DefaultEventSerializer(TestPrimitives.DefaultOptions, TypeMapper); services.AddSingleton(Serializer); services.AddSingleton(TypeMapper); diff --git a/src/Core/test/Eventuous.Tests.Subscriptions.Base/Fixtures/SubscriptionFixtureBase.cs b/src/Core/test/Eventuous.Tests.Subscriptions.Base/Fixtures/SubscriptionFixtureBase.cs index af24a8ab..e075516f 100644 --- a/src/Core/test/Eventuous.Tests.Subscriptions.Base/Fixtures/SubscriptionFixtureBase.cs +++ b/src/Core/test/Eventuous.Tests.Subscriptions.Base/Fixtures/SubscriptionFixtureBase.cs @@ -58,7 +58,7 @@ protected override void SetupServices(IServiceCollection services) { var host = services.First(x => !x.IsKeyedService && x.ImplementationFactory?.GetType() == typeof(Func)); services.Remove(host); - services.AddLogging(b => ConfigureLogging(b.ForTests(_logLevel))); + services.AddLogging(b => ConfigureLogging(b.ForTests(_logLevel)).SetMinimumLevel(_logLevel)); } protected override void GetDependencies(IServiceProvider provider) { diff --git a/src/Core/test/Eventuous.Tests.Subscriptions/SequenceTests.cs b/src/Core/test/Eventuous.Tests.Subscriptions/SequenceTests.cs index 13117167..a8f2681e 100644 --- a/src/Core/test/Eventuous.Tests.Subscriptions/SequenceTests.cs +++ b/src/Core/test/Eventuous.Tests.Subscriptions/SequenceTests.cs @@ -8,7 +8,7 @@ namespace Eventuous.Tests.Subscriptions; public class SequenceTests { public SequenceTests() { var factory = new LoggerFactory(); - factory.AddProvider(new TUnitLoggerProvider()); + factory.AddProvider(new TUnitLoggerProvider(LogLevel.Information)); var services = new ServiceCollection(); services.AddSingleton(factory); var provider = services.BuildServiceProvider(); diff --git a/src/Core/test/Eventuous.Tests.Subscriptions/TestContext.cs b/src/Core/test/Eventuous.Tests.Subscriptions/TestContext.cs index af068b76..dc7e90f7 100644 --- a/src/Core/test/Eventuous.Tests.Subscriptions/TestContext.cs +++ b/src/Core/test/Eventuous.Tests.Subscriptions/TestContext.cs @@ -23,7 +23,7 @@ public static class TestContext { CancellationToken.None ) ) - .RuleFor(x => x.LogContext, (_, _) => new("test", new LoggerFactory().AddTUnit())); + .RuleFor(x => x.LogContext, (_, _) => new("test", new LoggerFactory().AddTUnit(LogLevel.Information))); public static MessageConsumeContext CreateContext() => Auto.Generate(); } diff --git a/src/EventStore/test/Eventuous.Tests.EventStore/ProducerTracesTests.cs b/src/EventStore/test/Eventuous.Tests.EventStore/ProducerTracesTests.cs index 58346df1..caeaa37b 100644 --- a/src/EventStore/test/Eventuous.Tests.EventStore/ProducerTracesTests.cs +++ b/src/EventStore/test/Eventuous.Tests.EventStore/ProducerTracesTests.cs @@ -16,13 +16,13 @@ public TracesTests() : base(new()) { _listener = new() { ShouldListenTo = _ => true, Sample = (ref ActivityCreationOptions _) => ActivitySamplingResult.AllData, - ActivityStarted = activity => Log.LogInformation( + ActivityStarted = activity => Log.LogTrace( "Started {Activity} with {Id}, parent {ParentId}", activity.DisplayName, activity.Id, activity.ParentId ), - ActivityStopped = activity => Log.LogInformation("Stopped {Activity}", activity.DisplayName) + ActivityStopped = activity => Log.LogTrace("Stopped {Activity}", activity.DisplayName) }; ActivitySource.AddActivityListener(_listener); diff --git a/src/EventStore/test/Eventuous.Tests.EventStore/Subscriptions/Fixtures/LegacySubscriptionFixture.cs b/src/EventStore/test/Eventuous.Tests.EventStore/Subscriptions/Fixtures/LegacySubscriptionFixture.cs index d64c0501..103a6a41 100644 --- a/src/EventStore/test/Eventuous.Tests.EventStore/Subscriptions/Fixtures/LegacySubscriptionFixture.cs +++ b/src/EventStore/test/Eventuous.Tests.EventStore/Subscriptions/Fixtures/LegacySubscriptionFixture.cs @@ -16,7 +16,7 @@ public abstract class LegacySubscriptionFixture: IAsyncInitializer, IAsyncDis protected TestCheckpointStore CheckpointStore { get; } = new(); protected StreamSubscription Subscription { get; set; } = null!; - protected LegacySubscriptionFixture(T handler, StreamName? stream = null, LogLevel logLevel = LogLevel.Debug) { + protected LegacySubscriptionFixture(T handler, StreamName? stream = null, LogLevel logLevel = LogLevel.Information) { if (stream is { } s) Stream = s; LoggerFactory = LoggingExtensions.GetLoggerFactory(logLevel); diff --git a/test/Eventuous.TestHelpers.TUnit/Logging/LoggingExtensions.cs b/test/Eventuous.TestHelpers.TUnit/Logging/LoggingExtensions.cs index bdec4794..bc8927a4 100644 --- a/test/Eventuous.TestHelpers.TUnit/Logging/LoggingExtensions.cs +++ b/test/Eventuous.TestHelpers.TUnit/Logging/LoggingExtensions.cs @@ -1,33 +1,40 @@ +using System.Collections.Concurrent; using Microsoft.Extensions.Logging; namespace Eventuous.TestHelpers.TUnit.Logging; public static class LoggingExtensions { - public static ILoggerFactory GetLoggerFactory(LogLevel logLevel = LogLevel.Debug) + public static ILoggerFactory GetLoggerFactory(LogLevel logLevel = LogLevel.Information) => LoggerFactory.Create( builder => builder .SetMinimumLevel(logLevel) .AddFilter("Microsoft", LogLevel.Warning) .AddFilter("Grpc", LogLevel.Warning) - .AddTUnit() + .AddTUnit(logLevel) ); - - public static ILoggerFactory AddTUnit(this ILoggerFactory factory) { - factory.AddProvider(new TUnitLoggerProvider()); + + public static ILoggerFactory AddTUnit(this ILoggerFactory factory, LogLevel logLevel) { + factory.AddProvider(new TUnitLoggerProvider(logLevel)); return factory; } - public static ILoggingBuilder AddTUnit(this ILoggingBuilder builder) => builder.AddProvider(new TUnitLoggerProvider()); - - public static ILoggingBuilder ForTests(this ILoggingBuilder builder, LogLevel logLevel = LogLevel.Information) - => builder.AddTUnit().SetMinimumLevel(logLevel).AddFilter("Grpc", LogLevel.Warning).AddFilter("Microsoft", LogLevel.Warning); + public static ILoggingBuilder AddTUnit(this ILoggingBuilder builder, LogLevel logLevel) => builder.AddProvider(new TUnitLoggerProvider(logLevel)); + + public static ILoggingBuilder ForTests(this ILoggingBuilder builder, LogLevel logLevel = LogLevel.Information) + => builder + .AddTUnit(logLevel) + .SetMinimumLevel(logLevel) + .AddFilter("Grpc", LogLevel.Warning) + .AddFilter("Microsoft", LogLevel.Warning) + .AddFilter("Npgsql", LogLevel.Warning); } -public sealed class TUnitLoggerProvider() : ILoggerProvider { - private readonly LoggerExternalScopeProvider _scopeProvider = new(); +public sealed class TUnitLoggerProvider(LogLevel logLevel) : ILoggerProvider { + private readonly LoggerExternalScopeProvider _scopeProvider = new(); + private readonly ConcurrentDictionary _loggers = new(StringComparer.OrdinalIgnoreCase); - public ILogger CreateLogger(string categoryName) => new TUnitLog(_scopeProvider); + public ILogger CreateLogger(string categoryName) => _loggers.GetOrAdd(categoryName, name => new(_scopeProvider, name, logLevel)); - public void Dispose() { } + public void Dispose() => _loggers.Clear(); } diff --git a/test/Eventuous.TestHelpers.TUnit/Logging/TUnitLogger.cs b/test/Eventuous.TestHelpers.TUnit/Logging/TUnitLogger.cs index 010dc528..b85a5d38 100644 --- a/test/Eventuous.TestHelpers.TUnit/Logging/TUnitLogger.cs +++ b/test/Eventuous.TestHelpers.TUnit/Logging/TUnitLogger.cs @@ -4,15 +4,16 @@ namespace Eventuous.TestHelpers.TUnit.Logging; -public class TUnitLog(LoggerExternalScopeProvider scopeProvider) : ILogger { - public bool IsEnabled(LogLevel logLevel) => logLevel != LogLevel.None; +public class TUnitLog(LoggerExternalScopeProvider scopeProvider, string category, LogLevel logLevel) : ILogger { + public bool IsEnabled(LogLevel level) => level >= logLevel; public IDisposable? BeginScope(TState state) where TState : notnull => scopeProvider.Push(state); - public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter) { - if (TestContext.Current == null) return; - var level = logLevel switch { + public void Log(LogLevel l, EventId eventId, TState state, Exception? exception, Func formatter) { + if (TestContext.Current == null || !IsEnabled(l)) return; + + var level = l switch { LogLevel.Trace => global::TUnit.Core.Logging.LogLevel.Trace, LogLevel.Debug => global::TUnit.Core.Logging.LogLevel.Debug, LogLevel.Information => global::TUnit.Core.Logging.LogLevel.Information, @@ -21,6 +22,6 @@ public void Log(LogLevel logLevel, EventId eventId, TState state, Except LogLevel.Critical => global::TUnit.Core.Logging.LogLevel.Critical, _ => throw new ArgumentOutOfRangeException(nameof(logLevel)) }; - TestContext.Current.GetDefaultLogger().Log(level, state, exception, formatter); + TestContext.Current.OutputWriter.WriteLine($"[{category}] {level}: {formatter(state, exception)}"); } }