11using System ;
22using System . IO ;
3+ using Microsoft . AspNetCore ;
34using Microsoft . AspNetCore . Hosting ;
45using Microsoft . Extensions . Configuration ;
56using Serilog ;
@@ -8,16 +9,17 @@ namespace SimpleWebSample
89{
910 public class Program
1011 {
12+ public static IConfiguration Configuration { get ; } = new ConfigurationBuilder ( )
13+ . SetBasePath ( Directory . GetCurrentDirectory ( ) )
14+ . AddJsonFile ( "appsettings.json" , optional : false , reloadOnChange : true )
15+ . AddJsonFile ( $ "appsettings.{ Environment . GetEnvironmentVariable ( "ASPNETCORE_ENVIRONMENT" ) ?? "Production" } .json", optional : true )
16+ . Build ( ) ;
17+
1118 public static int Main ( string [ ] args )
1219 {
13- var configuration = new ConfigurationBuilder ( )
14- . SetBasePath ( Directory . GetCurrentDirectory ( ) )
15- . AddJsonFile ( "appsettings.json" , optional : false , reloadOnChange : true )
16- . AddJsonFile ( $ "appsettings.{ Environment . GetEnvironmentVariable ( "ASPNETCORE_ENVIRONMENT" ) ?? "Production" } .json", optional : true )
17- . Build ( ) ;
1820
1921 Log . Logger = new LoggerConfiguration ( )
20- . ReadFrom . Configuration ( configuration )
22+ . ReadFrom . Configuration ( Configuration )
2123 . Enrich . FromLogContext ( )
2224 . WriteTo . Console ( )
2325 . CreateLogger ( ) ;
@@ -26,16 +28,7 @@ public static int Main(string[] args)
2628 {
2729 Log . Information ( "Getting the motors running..." ) ;
2830
29- var host = new WebHostBuilder ( )
30- . UseKestrel ( )
31- . UseContentRoot ( Directory . GetCurrentDirectory ( ) )
32- . UseIISIntegration ( )
33- . UseStartup < Startup > ( )
34- . UseConfiguration ( configuration )
35- . UseSerilog ( )
36- . Build ( ) ;
37-
38- host . Run ( ) ;
31+ BuildWebHost ( args ) . Run ( ) ;
3932
4033 return 0 ;
4134 }
@@ -49,5 +42,15 @@ public static int Main(string[] args)
4942 Log . CloseAndFlush ( ) ;
5043 }
5144 }
45+
46+ public static IWebHost BuildWebHost ( string [ ] args ) =>
47+ WebHost . CreateDefaultBuilder ( args )
48+ . UseStartup < Startup > ( )
49+ . UseConfiguration ( Configuration )
50+ . UseSerilog ( ( context , configuration ) =>
51+ configuration . ReadFrom . Configuration ( Configuration )
52+ . Enrich . FromLogContext ( )
53+ . WriteTo . Console ( ) )
54+ . Build ( ) ;
5255 }
5356}
0 commit comments