11using Microsoft . AspNetCore ;
22using Microsoft . AspNetCore . Hosting ;
3+ using Microsoft . eShopOnContainers . WebMVC ;
34using Microsoft . Extensions . Configuration ;
45using Microsoft . Extensions . Logging ;
56using Serilog ;
67using System ;
78using System . IO ;
89
9- namespace Microsoft . eShopOnContainers . WebMVC
10- {
11- public class Program
12- {
13- public static readonly string Namespace = typeof ( Program ) . Namespace ;
14- public static readonly string AppName = Namespace . Substring ( Namespace . LastIndexOf ( '.' , Namespace . LastIndexOf ( '.' ) - 1 ) + 1 ) ;
10+ var configuration = GetConfiguration ( ) ;
1511
16- public static int Main ( string [ ] args )
17- {
18- var configuration = GetConfiguration ( ) ;
12+ Log . Logger = CreateSerilogLogger ( configuration ) ;
1913
20- Log . Logger = CreateSerilogLogger ( configuration ) ;
14+ try
15+ {
16+ Log . Information ( "Configuring web host ({ApplicationContext})..." , Program . AppName ) ;
17+ var host = BuildWebHost ( configuration , args ) ;
2118
22- try
23- {
24- Log . Information ( "Configuring web host ({ApplicationContext})..." , AppName ) ;
25- var host = BuildWebHost ( configuration , args ) ;
19+ Log . Information ( "Starting web host ({ApplicationContext})..." , Program . AppName ) ;
20+ host . Run ( ) ;
2621
27- Log . Information ( "Starting web host ({ApplicationContext})..." , AppName ) ;
28- host . Run ( ) ;
22+ return 0 ;
23+ }
24+ catch ( Exception ex )
25+ {
26+ Log . Fatal ( ex , "Program terminated unexpectedly ({ApplicationContext})!" , Program . AppName ) ;
27+ return 1 ;
28+ }
29+ finally
30+ {
31+ Log . CloseAndFlush ( ) ;
32+ }
33+
34+ IWebHost BuildWebHost ( IConfiguration configuration , string [ ] args ) =>
35+ WebHost . CreateDefaultBuilder ( args )
36+ . CaptureStartupErrors ( false )
37+ . ConfigureAppConfiguration ( x => x . AddConfiguration ( configuration ) )
38+ . UseStartup < Startup > ( )
39+ . UseSerilog ( )
40+ . Build ( ) ;
2941
30- return 0 ;
31- }
32- catch ( Exception ex )
33- {
34- Log . Fatal ( ex , "Program terminated unexpectedly ({ApplicationContext})!" , AppName ) ;
35- return 1 ;
36- }
37- finally
38- {
39- Log . CloseAndFlush ( ) ;
40- }
41- }
42+ Serilog . ILogger CreateSerilogLogger ( IConfiguration configuration )
43+ {
44+ var seqServerUrl = configuration [ "Serilog:SeqServerUrl" ] ;
45+ var logstashUrl = configuration [ "Serilog:LogstashgUrl" ] ;
46+ var cfg = new LoggerConfiguration ( )
47+ . ReadFrom . Configuration ( configuration )
48+ . Enrich . WithProperty ( "ApplicationContext" , Program . AppName )
49+ . Enrich . FromLogContext ( )
50+ . WriteTo . Console ( ) ;
51+ if ( ! string . IsNullOrWhiteSpace ( seqServerUrl ) )
52+ {
53+ cfg . WriteTo . Seq ( seqServerUrl ) ;
54+ }
55+ if ( ! string . IsNullOrWhiteSpace ( logstashUrl ) )
56+ {
57+ cfg . WriteTo . Http ( logstashUrl ) ;
58+ }
59+ return cfg . CreateLogger ( ) ;
60+ }
4261
43- private static IWebHost BuildWebHost ( IConfiguration configuration , string [ ] args ) =>
44- WebHost . CreateDefaultBuilder ( args )
45- . CaptureStartupErrors ( false )
46- . ConfigureAppConfiguration ( x => x . AddConfiguration ( configuration ) )
47- . UseStartup < Startup > ( )
48- . UseSerilog ( )
49- . Build ( ) ;
62+ IConfiguration GetConfiguration ( )
63+ {
64+ var builder = new ConfigurationBuilder ( )
65+ . SetBasePath ( Directory . GetCurrentDirectory ( ) )
66+ . AddJsonFile ( "appsettings.json" , optional : false , reloadOnChange : true )
67+ . AddEnvironmentVariables ( ) ;
5068
51- private static Serilog . ILogger CreateSerilogLogger ( IConfiguration configuration )
52- {
53- var seqServerUrl = configuration [ "Serilog:SeqServerUrl" ] ;
54- var logstashUrl = configuration [ "Serilog:LogstashgUrl" ] ;
55- var cfg = new LoggerConfiguration ( )
56- . ReadFrom . Configuration ( configuration )
57- . Enrich . WithProperty ( "ApplicationContext" , AppName )
58- . Enrich . FromLogContext ( )
59- . WriteTo . Console ( ) ;
60- if ( ! string . IsNullOrWhiteSpace ( seqServerUrl ) ) {
61- cfg . WriteTo . Seq ( seqServerUrl ) ;
62- }
63- if ( ! string . IsNullOrWhiteSpace ( logstashUrl ) ) {
64- cfg . WriteTo . Http ( logstashUrl ) ;
65- }
66- return cfg . CreateLogger ( ) ;
67- }
69+ return builder . Build ( ) ;
70+ }
6871
69- private static IConfiguration GetConfiguration ( )
70- {
71- var builder = new ConfigurationBuilder ( )
72- . SetBasePath ( Directory . GetCurrentDirectory ( ) )
73- . AddJsonFile ( "appsettings.json" , optional : false , reloadOnChange : true )
74- . AddEnvironmentVariables ( ) ;
7572
76- return builder . Build ( ) ;
77- }
78- }
73+ public class Program
74+ {
75+ private static readonly string _namespace = typeof ( Startup ) . Namespace ;
76+ public static readonly string AppName = _namespace . Substring ( _namespace . LastIndexOf ( '.' , _namespace . LastIndexOf ( '.' ) - 1 ) + 1 ) ;
7977}
0 commit comments