Skip to content
This repository has been archived by the owner on Dec 19, 2018. It is now read-only.

Pass existing LoggerFactory to WebHostBuilder #658

Closed
cwe1ss opened this issue Mar 17, 2016 · 3 comments
Closed

Pass existing LoggerFactory to WebHostBuilder #658

cwe1ss opened this issue Mar 17, 2016 · 3 comments
Assignees
Milestone

Comments

@cwe1ss
Copy link
Contributor

cwe1ss commented Mar 17, 2016

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!

@cwe1ss cwe1ss changed the title Pass LoggerFactory to WebHostBuilder Pass existing LoggerFactory to WebHostBuilder Mar 17, 2016
@cwe1ss
Copy link
Contributor Author

cwe1ss commented Mar 17, 2016

Of course, we could also just use the existing constructor with an optional parameter:

public WebHostBuilder(ILoggerFactory loggerFactory = null)
{
     _hostingEnvironment = new HostingEnvironment();
     _loggerFactory = loggerFactory ?? new LoggerFactory();
     _configureServicesDelegates = new List<Action<IServiceCollection>>();
}

@cwe1ss
Copy link
Contributor Author

cwe1ss commented Mar 19, 2016

Closed to continue discussion directly in the pull request. I hope that's ok - I haven't figured out yet, whether it's better to have discussions in an issue or PR.

@cwe1ss cwe1ss closed this as completed Mar 19, 2016
@JunTaoLuo
Copy link
Contributor

@cwe1ss Don't close the issue until you have merged the PR. We use issues to track progress. Discussions can happen in both places though it's more design oriented in issues and more implementation oriented in PRs

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants