Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Allow an ILogger to be specified #183

Merged
merged 1 commit into from
May 19, 2020
Merged

Allow an ILogger to be specified #183

merged 1 commit into from
May 19, 2020

Conversation

nblumhardt
Copy link
Member

@nblumhardt nblumhardt commented Mar 25, 2020

Resolves #182

/// The logger through which request completion events will be logged. The default is to use the
/// static <see cref="Log"/> class.
/// </summary>
public ILogger Logger { get; set; }
Copy link
Contributor

Choose a reason for hiding this comment

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

https://github.com/serilog/serilog-aspnetcore/blob/dev/src/Serilog.AspNetCore/SerilogWebHostBuilderExtensions.cs#L146

I think it is worth to use DI provided features. Why to manually specify logger if it is already exists ? Just add new ctor param:

 public RequestLoggingMiddleware(RequestDelegate next, DiagnosticContext diagnosticContext, RequestLoggingOptions options, ILogger logger)

Copy link

@dazinator dazinator Mar 26, 2020

Choose a reason for hiding this comment

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

@sungam3r - what about if serilog ILogger is not registered for DI and the developer is only using Log.Logger everywhere - which i may be wrong, but is a valid style for using serilog?

Copy link
Contributor

Choose a reason for hiding this comment

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

Well, then we can use second RequestLoggingMiddleware ctor with ILogger property inside options to satisfy both categories.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the feedback @sungam3r - will give it some thought 👍

@pnmcosta
Copy link

Any prediction when this might be merged? Thanks.

@nblumhardt
Copy link
Member Author

@pnmcosta still needs some thought, sorry. I like the idea of accepting the logger as a constructor parameter, but I'm not sure whether this covers all scenarios 🤔

@pnmcosta
Copy link

pnmcosta commented May 19, 2020

@nblumhardt my scenario is that we built a library that extends Serilog with our own conventions, and we expose our own singleton for the root logger, not relying on Serilog's.

We also only leverage DI for MS's ILogger's, i'm not even sure if internally Serilog.Ilogger's are registered in the container, but in our case we don't need the Serilog.ILogger in the container.

From a consuming point of view, I'd definitely be more inclined to set the logger on the options, unless UseSerilog does it for us, as I am setting our singleton there.

HTH

@nblumhardt nblumhardt marked this pull request as ready for review May 19, 2020 21:00
@nblumhardt nblumhardt changed the title Spike for #182, allow an ILogger to be specified Allow an ILogger to be specified May 19, 2020
@nblumhardt nblumhardt merged commit 58e8431 into dev May 19, 2020
@nblumhardt nblumhardt mentioned this pull request Jul 24, 2020
@nblumhardt nblumhardt deleted the provide-ilogger branch March 3, 2021 23:10
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Middleware relies on static Log.Logger being set first.
4 participants