Skip to content
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

[CORELIB-75] DI injected logger #757

Open
wants to merge 11 commits into
base: v9.0-preview
Choose a base branch
from

Conversation

wojtek2288
Copy link
Contributor

No description provided.

Copy link

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
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
Copy link
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 CQRSMetrics metrics;
private readonly ISerializer serializer;
private readonly RequestDelegate next;

public CQRSMiddleware(CQRSMetrics metrics, ISerializer serializer, RequestDelegate next)
public CQRSMiddleware(
Copy link
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
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
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
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
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.

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