1- using Microsoft . AspNetCore ;
1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . IO ;
4+ using System . Linq ;
5+ using System . Threading . Tasks ;
6+ using Microsoft . AspNetCore ;
27using Microsoft . AspNetCore . Hosting ;
38using Microsoft . Extensions . Configuration ;
4- using Microsoft . Extensions . Configuration . Json ;
9+ using Microsoft . Extensions . Logging ;
510using Serilog ;
6- using System ;
7- using System . IO ;
811
912namespace Microsoft . eShopOnContainers . Mobile . Shopping . HttpAggregator
1013{
1114 public class Program
1215 {
13- public static readonly string AppName = typeof ( Program ) . Namespace ;
14- public static readonly string ShortAppName = AppName . Substring ( AppName . LastIndexOf ( '.' , AppName . LastIndexOf ( '.' ) - 1 ) + 1 ) ;
15-
16- public static int Main ( string [ ] args )
16+ public static void Main ( string [ ] args )
1717 {
18- var configuration = GetConfiguration ( ) ;
19-
20- Log . Logger = CreateSerilogLogger ( configuration ) ;
21-
22- try
23- {
24- Log . Information ( "Configuring web host ({Application})..." , AppName ) ;
25- var host = BuildWebHost ( configuration , args ) ;
26-
27- Log . Information ( "Starting web host ({Application})..." , AppName ) ;
28- host . Run ( ) ;
29-
30- return 0 ;
31- }
32- catch ( Exception ex )
33- {
34- Log . Fatal ( ex , "Program terminated unexpectedly ({Application})!" , AppName ) ;
35- return 1 ;
36- }
37- finally
38- {
39- Log . CloseAndFlush ( ) ;
40- }
18+ BuildWebHost ( args ) . Run ( ) ;
4119 }
4220
43- private static IWebHost BuildWebHost ( IConfiguration configuration , string [ ] args ) =>
44- WebHost . CreateDefaultBuilder ( args )
45- . CaptureStartupErrors ( false )
21+ public static IWebHost BuildWebHost ( string [ ] args ) =>
22+ WebHost
23+ . CreateDefaultBuilder ( args )
24+ . ConfigureAppConfiguration ( cb =>
25+ {
26+ var sources = cb . Sources ;
27+ sources . Insert ( 3 , new Microsoft . Extensions . Configuration . Json . JsonConfigurationSource ( )
28+ {
29+ Optional = true ,
30+ Path = "appsettings.localhost.json" ,
31+ ReloadOnChange = false
32+ } ) ;
33+ } )
4634 . UseStartup < Startup > ( )
47- . UseSerilog ( )
35+ . UseSerilog ( ( builderContext , config ) =>
36+ {
37+ config
38+ . MinimumLevel . Information ( )
39+ . Enrich . FromLogContext ( )
40+ . WriteTo . Console ( ) ;
41+ } )
4842 . Build ( ) ;
49-
50- private static Serilog . ILogger CreateSerilogLogger ( IConfiguration configuration )
51- {
52- var seqServerUrl = configuration [ "Serilog:SeqServerUrl" ] ;
53-
54- return new LoggerConfiguration ( )
55- . MinimumLevel . Verbose ( )
56- . Enrich . WithProperty ( "Application" , AppName )
57- . Enrich . FromLogContext ( )
58- . WriteTo . Console ( )
59- . WriteTo . Seq ( string . IsNullOrWhiteSpace ( seqServerUrl ) ? "http://seq" : seqServerUrl )
60- . ReadFrom . Configuration ( configuration )
61- . CreateLogger ( ) ;
62- }
63-
64- private static IConfiguration GetConfiguration ( )
65- {
66- var builder = new ConfigurationBuilder ( )
67- . SetBasePath ( Directory . GetCurrentDirectory ( ) )
68- . AddJsonFile ( "appsettings.json" , optional : false , reloadOnChange : true )
69- . AddEnvironmentVariables ( ) ;
70-
71- builder . Sources . Insert ( 3 , new JsonConfigurationSource ( )
72- {
73- Optional = true ,
74- Path = "appsettings.localhost.json" ,
75- ReloadOnChange = false
76- } ) ;
77-
78- return builder . Build ( ) ;
79- }
8043 }
81- }
44+ }
0 commit comments