-
Notifications
You must be signed in to change notification settings - Fork 4
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
base: v9.0-preview
Are you sure you want to change the base?
Conversation
test: Run #2139
🎉 All tests passed!🔄 This comment has been updated |
Let's also discuss this internally in a wider group. |
src/CQRS/LeanCode.CQRS.AspNetCore/Middleware/CQRSExceptionTranslationMiddleware.cs
Outdated
Show resolved
Hide resolved
3534e20
to
9b3d38a
Compare
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.
A couple of changes are needed:
- We must not start
Host
in tests that just configureServiceCollection
. This has much more consequences (it really starts a host). - 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 introduceAddLeanCodeLogger
that configures theILogger<T>
factory (+ Serilog). - We need
NullLogger<T>
- 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( |
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.
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).
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); |
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.
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() |
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.
This is wrong. We don't want to start host here, in any way.
|
||
public StubAuditLogStorage() | ||
{ | ||
logger = Substitute.For<Logging.ILogger<StubAuditLogStorage>>(); |
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.
We probably should have a NullLogger<T>
implementation. Using NSubstitute for this gives a wrong impression that it is used anyhow.
private static string GetLogOutput(Action log) | ||
{ | ||
using var stringWriter = new StringWriter(); | ||
|
||
Console.SetOut(stringWriter); | ||
|
||
log(); | ||
|
||
return stringWriter.ToString(); | ||
} |
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.
This will make the test output broken. We can't override console stream.
No description provided.