-
Notifications
You must be signed in to change notification settings - Fork 247
Consider a way to configure logging provider using the DI system #346
Comments
All of those changes would make the logging configuration more complex, and also defer when logging "kicks in". By adding the logging provider as early as the startup class ctor in plain-old-c# you can also write messages in the configure services method, much earlier than when the options system and IOC is active. Also with the Program.Main hosting model we might also want to add a way to pass an application-created ILoggerFactory in to the web host builder. So yeah, end of the day I think a number of subsystems like IFileProvider, IConfiguration, ILoggerFactory are designed with IoC in mind, and may be added like services, but it's not a good idea to go the rest of the way and make their activation and building depend on DI or options. |
That doesn't need to change. You can still inject the
That doesn't make it any cleaner but we should allow for that as well.
|
@lodejard What if we make this a concern of Hosting instead? We resolve |
I could see that as an extra step we add, right after the host gets control back from ConfigureServices and creates the service provider? |
Yep |
@davidfowl @lodejard do we have an agreement here? cc @Eilon |
+10! I think it would be great if Logging could be configured as early as possible. Ideally it should happen as the first thing in Program.cs to be able to log initialization exceptions. Just think of the case where you start the web application on a port that's already taken. It's not possible to log this right now, right? |
It's a reasonable thing to do, resolving ILoggerProvider from IoC after ConfigureServices returns. Though if you want the logging as soon as possible you'd still take ILoggerFactory as a Startup class constructor argument and add providers there. @cwe1ss that would log server startup exceptions also. |
I didn't know that this works! great, thx! |
Does aspnet/Hosting#661 resolve this? |
@lodejard can you send me code to do this with the RC2 bits. The template generated code doesn't hook up logging until Should I open a bug with the template for initializing logging so late in the game? |
On second thought, no thank you on option 2 -- that would mean you'd have to implement |
Yeah, both option 1 and 2 leave something to be desired. THB this should be a hosting feature - after the application container is returned, the hosting library should resolve |
That said - I'm actually in favor of Option 4 - don't change it. This is actually one of the few cases DI population has some disadvantages - the sooner you add providers to logging the more information you will have re: your startup sequence. For example - if components are sending log messages during ConfigureServices - or if your IoC container population or resolution is throwing exceptions - you'll never see any of that if your logging providers are resolved out of your IoC container. |
Not all logging providers are created equal. During application startup, it may be beneficial to have a trace of critical information using a 'simple' provider like the console provider, but once the application has started, it can be far more valuable to stream logs into some kind of database like SQL Server or Elasticsearch -- such 'complex' providers could really make good use of DI. So basically what @davidfowl said:
|
Okay, but isn't that already totally do-able? https://gist.github.com/lodejard/93f00f23bd8ee3f5f9fb64232e9503a9 My File->New project template is a bit out of date, but you get the idea. |
@lodejard that would indeed work, but the |
What is the status of this proposal? |
I dont think this should be done... logging with some providers should be configured before startup and DI even runs else you miss startup / DI config errors ! So we setup the logging in Program.cs and it then add it to the services injected in the webbuilder it than becomes available in Startup if people need it. |
We're pursuing this for 2.0 |
@pakrym can we close this now? |
This code always looks a little bizzare:
The fact that hosting adds the default ILoggerFactory is fine, but having to resolve it in Configure to add logging providers seems anti to how we've integrated the rest of the stack (it feels bolted on). Also, configuring logging in ConfigureServices would mean the only thing we configure in Configure would be the http stuff (for the most part).
Proposal 1
The default logger factory should resolve an IEnumerable in its ctor.
The only downside if that more extension methods get added to
IServiceCollection
but meh.Proposal 2
Add
LoggerOptions
and hang an IList off it./cc @DamianEdwards @lodejard @rynowak
The text was updated successfully, but these errors were encountered: