.NET Core 3.1.1--CreateDefaultBuilder(args) 方法源码

 1 public static IHostBuilder CreateDefaultBuilder(string[] args)
 2 {
 3     HostBuilder hostBuilder = new HostBuilder();
 4     hostBuilder.UseContentRoot(Directory.GetCurrentDirectory());
 5     hostBuilder.ConfigureHostConfiguration(delegate(IConfigurationBuilder config)
 6     {
 7         config.AddEnvironmentVariables("DOTNET_");
 8         if (args != null)
 9         {
10             config.AddCommandLine(args);
11         }
12     });
13     hostBuilder.ConfigureAppConfiguration(delegate(HostBuilderContext hostingContext, IConfigurationBuilder config)
14     {
15         IHostEnvironment hostingEnvironment = hostingContext.HostingEnvironment;
16         config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).AddJsonFile("appsettings." + hostingEnvironment.EnvironmentName + ".json", optional: true, reloadOnChange: true);
17         if (hostingEnvironment.IsDevelopment() && !string.IsNullOrEmpty(hostingEnvironment.ApplicationName))
18         {
19             Assembly assembly = Assembly.Load(new AssemblyName(hostingEnvironment.ApplicationName));
20             if (assembly != null)
21             {
22                 config.AddUserSecrets(assembly, optional: true);
23             }
24         }
25         config.AddEnvironmentVariables();
26         if (args != null)
27         {
28             config.AddCommandLine(args);
29         }
30     }).ConfigureLogging(delegate(HostBuilderContext hostingContext, ILoggingBuilder logging)
31     {
32         bool num = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
33         if (num)
34         {
35             logging.AddFilter<EventLogLoggerProvider>((LogLevel level) => level >= LogLevel.Warning);
36         }
37         logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
38         logging.AddConsole();
39         logging.AddDebug();
40         logging.AddEventSourceLogger();
41         if (num)
42         {
43             logging.AddEventLog();
44         }
45     }).UseDefaultServiceProvider(delegate(HostBuilderContext context, ServiceProviderOptions options)
46     {
47         bool validateOnBuild = options.ValidateScopes = context.HostingEnvironment.IsDevelopment();
48         options.ValidateOnBuild = validateOnBuild;
49     });
50     return hostBuilder;
51 }

猜你喜欢

转载自www.cnblogs.com/YourDirection/p/12446821.html