Skip to content

[CORELIB-75] DI injected logger#757

Closed
wojtek2288 wants to merge 11 commits into
v9.0-previewfrom
feature/di-injected-logger
Closed

[CORELIB-75] DI injected logger#757
wojtek2288 wants to merge 11 commits into
v9.0-previewfrom
feature/di-injected-logger

Conversation

@wojtek2288
Copy link
Copy Markdown
Contributor

No description provided.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Feb 7, 2025

test: Run #2139

Tests 📝 Passed ✅ Failed ❌ Skipped ⏭️ Pending ⏳ Other ❓ Flaky 🍂 Duration ⏱️
749 748 0 1 0 0 0 271ms

🎉 All tests passed!

Github Test Reporter

🔄 This comment has been updated

@jakubfijalkowski
Copy link
Copy Markdown
Member

Let's also discuss this internally in a wider group.

@wojtek2288 wojtek2288 force-pushed the feature/di-injected-logger branch from 3534e20 to 9b3d38a Compare February 24, 2025 09:10
Comment thread src/Infrastructure/LeanCode.Logging/IHostBuilderExtensions.cs Outdated
Comment thread src/Infrastructure/LeanCode.Logging/IHostBuilderExtensions.cs Outdated
Copy link
Copy Markdown
Member

@jakubfijalkowski jakubfijalkowski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of changes are needed:

  1. We must not start Host in tests that just configure ServiceCollection. This has much more consequences (it really starts a host).
  2. We need to split how we configure the logger, because currently it is very awkward (esp. for tests). We need to leave ConfigureDefaultLogger as-is, but we need to introduce AddLeanCodeLogger that configures the ILogger<T> factory (+ Serilog).
  3. We need NullLogger<T>
  4. We probably should split the LeanCode.Logging package into two - one that has the interface (+ NullLogger<T>), and the other that has the configuration methods. Now, we have quite unexpected dependencies from e.g. from LeanCode.CQRS.Seciruty to Serilog.Sinks.Console.

private readonly RequestDelegate next;

public CQRSMiddleware(CQRSMetrics metrics, ISerializer serializer, RequestDelegate next)
public CQRSMiddleware(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One last (not priority, not mandatory) request - let's put the logger as a last parameter (I think this is also a convention in the .NET examples, but might be wrong here).

Comment on lines +95 to +106
var logger = loggerConfiguration.CreateLogger();

if (preserveStaticLogger)
{
Log.Logger = logger;
}

logging.Services.AddSingleton<Serilog.ILogger>(logger);
logging.Services.AddSingleton(typeof(ILogger<>), typeof(Logger<>));

logging.AddConfiguration(configuration.GetSection(SystemLoggersEntryName));
logging.AddSerilog();
logging.AddConfiguration(context.Configuration.GetSection(SystemLoggersEntryName));
logging.AddSerilog(logger, dispose: true);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I know why we haven't used AddSerilog in the ConfigureServices, but let's be sure - why?

serviceCollection.AddScoped<LocalHandlerMiddleware>();
serviceCollection.AddScoped<CQRSMetrics>();
serviceCollection.AddScoped<ICommandValidatorResolver, CommandValidatorResolver>();
var hostBuilder = Host.CreateDefaultBuilder()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong. We don't want to start host here, in any way.


public StubAuditLogStorage()
{
logger = Substitute.For<Logging.ILogger<StubAuditLogStorage>>();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably should have a NullLogger<T> implementation. Using NSubstitute for this gives a wrong impression that it is used anyhow.

Comment on lines +50 to +59
private static string GetLogOutput(Action log)
{
using var stringWriter = new StringWriter();

Console.SetOut(stringWriter);

log();

return stringWriter.ToString();
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will make the test output broken. We can't override console stream.

@jakubfijalkowski
Copy link
Copy Markdown
Member

Superseded by #775

@codecov
Copy link
Copy Markdown

codecov Bot commented Jun 10, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 0.00%. Comparing base (97eeedb) to head (d02671c).
Report is 30 commits behind head on v9.0-preview.

Additional details and impacted files
@@         Coverage Diff         @@
##   v9.0-preview   #757   +/-   ##
===================================
===================================

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jakubfijalkowski jakubfijalkowski deleted the feature/di-injected-logger branch November 12, 2025 11:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants