1
1
using System ;
2
2
using System . IO ;
3
+ using Microsoft . AspNetCore ;
3
4
using Microsoft . AspNetCore . Hosting ;
4
5
using Microsoft . Extensions . Configuration ;
5
6
using Serilog ;
@@ -8,16 +9,17 @@ namespace SimpleWebSample
8
9
{
9
10
public class Program
10
11
{
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
+
11
18
public static int Main ( string [ ] args )
12
19
{
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 ( ) ;
18
20
19
21
Log . Logger = new LoggerConfiguration ( )
20
- . ReadFrom . Configuration ( configuration )
22
+ . ReadFrom . Configuration ( Configuration )
21
23
. Enrich . FromLogContext ( )
22
24
. WriteTo . Console ( )
23
25
. CreateLogger ( ) ;
@@ -26,16 +28,7 @@ public static int Main(string[] args)
26
28
{
27
29
Log . Information ( "Getting the motors running..." ) ;
28
30
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 ( ) ;
39
32
40
33
return 0 ;
41
34
}
@@ -49,5 +42,12 @@ public static int Main(string[] args)
49
42
Log . CloseAndFlush ( ) ;
50
43
}
51
44
}
45
+
46
+ public static IWebHost BuildWebHost ( string [ ] args ) =>
47
+ WebHost . CreateDefaultBuilder ( args )
48
+ . UseStartup < Startup > ( )
49
+ . UseConfiguration ( Configuration )
50
+ . UseSerilog ( )
51
+ . Build ( ) ;
52
52
}
53
53
}
0 commit comments