33using Microsoft . Extensions . Configuration ;
44using Microsoft . Extensions . Logging ;
55using Serilog ;
6+ using System ;
67using System . IO ;
78
89namespace Microsoft . eShopOnContainers . WebMVC
910{
1011 public class Program
1112 {
12- public static void Main ( string [ ] args )
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 )
1317 {
14- BuildWebHost ( args ) . Run ( ) ;
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+ }
1541 }
1642
17- public static IWebHost BuildWebHost ( string [ ] args ) =>
43+ private static IWebHost BuildWebHost ( IConfiguration configuration , string [ ] args ) =>
1844 WebHost . CreateDefaultBuilder ( args )
19- . UseContentRoot ( Directory . GetCurrentDirectory ( ) )
45+ . CaptureStartupErrors ( false )
2046 . UseStartup < Startup > ( )
21- . ConfigureAppConfiguration ( ( builderContext , config ) =>
22- {
23- config . AddEnvironmentVariables ( ) ;
24- } )
25- . ConfigureLogging ( ( hostingContext , builder ) =>
26- {
27- builder . AddConfiguration ( hostingContext . Configuration . GetSection ( "Logging" ) ) ;
28- builder . AddConsole ( ) ;
29- builder . AddDebug ( ) ;
30- } )
3147 . UseApplicationInsights ( )
32- . UseSerilog ( ( builderContext , config ) =>
33- {
34- config
35- . MinimumLevel . Information ( )
36- . Enrich . FromLogContext ( )
37- . WriteTo . Console ( ) ;
38- } )
48+ . UseConfiguration ( configuration )
49+ . UseSerilog ( )
3950 . Build ( ) ;
51+
52+ private static Serilog . ILogger CreateSerilogLogger ( IConfiguration configuration )
53+ {
54+ var seqServerUrl = configuration [ "Serilog:SeqServerUrl" ] ;
55+
56+ return new LoggerConfiguration ( )
57+ . MinimumLevel . Verbose ( )
58+ . Enrich . WithProperty ( "Application" , AppName )
59+ . Enrich . FromLogContext ( )
60+ . WriteTo . Console ( )
61+ . WriteTo . Seq ( string . IsNullOrWhiteSpace ( seqServerUrl ) ? "http://seq" : seqServerUrl )
62+ . ReadFrom . Configuration ( configuration )
63+ . CreateLogger ( ) ;
64+ }
65+
66+ private static IConfiguration GetConfiguration ( )
67+ {
68+ var builder = new ConfigurationBuilder ( )
69+ . SetBasePath ( Directory . GetCurrentDirectory ( ) )
70+ . AddJsonFile ( "appsettings.json" , optional : false , reloadOnChange : true )
71+ . AddEnvironmentVariables ( ) ;
72+
73+ return builder . Build ( ) ;
74+ }
4075 }
41- }
76+ }
0 commit comments