weixin_39662834
weixin_39662834
2020-12-27 21:58

First class Configuration and Logging in WebHostBuilder

We want to enable the following program main:

csharp
public static void Main(string[] args)
        {
            var host = new WebHostBuilder()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseConfiguration((configBuilder, env) => 
                {
                    configBuilder.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                                 .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                                 .AddEnvironmentVariables()
                                 .AddCommandLine(args);
                })
                .UseLogging((factory, config) =>
                {
                    factory.AddFilter(config.GetSection("Logging"));
                    factory.AddConsole();
                    factory.AddDebug();
                })
                .UseIISIntegration()
                .UseStartup<startup>()
                .UseKestrel()
                .Build();

            host.Run();
        }
</startup>

This issue is to track the WebHostBuilder part of this.

该提问来源于开源项目:aspnet/Hosting

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

7条回答

  • weixin_39662834 weixin_39662834 4月前

    Having IConfiguration in DI doesn't stop you from using multiple, but you would need to define them yourselves and pick which one is the one that goes into DI. Ideally use Options as well instead of Configuration directly.

    点赞 评论 复制链接分享
  • weixin_39571219 weixin_39571219 4月前

    Yep, just wanted to point it out... it might not be an issue. Will you add IConfigurationRoot and IConfiguration? IConfiguration seems nicer to me for DI because it's the more generic interface. People who use 3rd party DI containers could probably do more stuff with it (e.g. Injecting just a section).

    Not completely obvious in this, but relevent to you is that this will play into the other issue about deprecating AddProvider on LoggerFactory and allowing Serilog, NLog, etc to replace the LoggerFactory instead of having a provider.

    Awesome!!

    点赞 评论 复制链接分享
  • weixin_39571219 weixin_39571219 4月前

    PS: I'm using Options most of the time, yes. So again, not sure how important the IConfiguration scenario really is.

    点赞 评论 复制链接分享
  • weixin_39528843 weixin_39528843 4月前
    • With an eye to removing these parts from Startup and exposing config via DI.
    点赞 评论 复制链接分享
  • weixin_39571219 weixin_39571219 4月前

    Sounds great! How will users be able to get the configuration object?

    点赞 评论 复制链接分享
  • weixin_39571219 weixin_39571219 4月前

    With an eye to removing these parts from Startup and exposing config via DI.

    Would I be able to resolve IConfiguration or would it be a separate type? My concern with IConfiguration are scenarios where applications use multiple independent configuration objects. (e.g. one for appSettings and one for secrets from Azure Key Vault). It might not be clear what gets injected when people use IConfiguration. Not sure if this is a real concern though?!

    点赞 评论 复制链接分享
  • weixin_39662834 weixin_39662834 4月前

    At the moment I think we will just put IConfigurationRoot in DI. We are hearing from many people that it's what they want.

    Not completely obvious in this, but relevent to you is that this will play into the other issue about deprecating AddProvider on LoggerFactory and allowing Serilog, NLog, etc to replace the LoggerFactory instead of having a provider.

    点赞 评论 复制链接分享

相关推荐