Pass existing LoggerFactory to WebHostBuilder #658
Description
Initializing the logging system should be one of the first things in an app. Now the LoggerFactory
is created in the constructor of WebHostBuilder
and it can be configured by calling webHostBuilder.ConfigureLogging
or by configuring the logger in your Startup
class.
However, there are scenarios where you want to initialize your logging system before creating a WebHostBuilder
:
- your application hosts multiple WebHosts
- some stuff happens before you create the WebHost (e.g. registering with and getting the hosting url from Service Fabric)
- your app might be a GUI app that just happens to host a WebAPI for whatever reason, ...
For this reason, I suggest that it should be possible to optionally pass an existing LoggerFactory
into the WebHostBuilder
. This could be done by introducing a second constructor WebHostBuilder(ILoggerFactory loggerFactory)
or by introducing another method .UseLoggerFactory(ILoggerFactory loggerFactory)
.
I would prefer the second constructor because this makes sure that there's always just one instance of the factory and you can't mix up the calls to UseLoggerFactory
and ConfigureLogging
.
If you agree to this, I'd be happy to create a pull request!