1
- var configuration = GetConfiguration ( ) ;
1
+ try
2
+ {
3
+ var builder = WebApplication . CreateBuilder ( args ) ;
4
+ if ( builder . Configuration . GetValue < bool > ( "UseVault" , false ) )
5
+ {
6
+ TokenCredential credential = new ClientSecretCredential (
7
+ builder . Configuration [ "Vault:TenantId" ] ,
8
+ builder . Configuration [ "Vault:ClientId" ] ,
9
+ builder . Configuration [ "Vault:ClientSecret" ] ) ;
10
+ builder . Configuration . AddAzureKeyVault ( new Uri ( $ "https://{ builder . Configuration [ "Vault:Name" ] } .vault.azure.net/") , credential ) ;
11
+ }
2
12
3
- Log . Logger = CreateSerilogLogger ( configuration ) ;
13
+ builder . Host . UseSerilog ( CreateLogger ( builder . Configuration ) ) ;
14
+ builder . WebHost . CaptureStartupErrors ( false ) ;
15
+ builder . Services . AddApplicationInsightsTelemetry ( builder . Configuration ) ;
16
+ builder . Services . AddApplicationInsightsKubernetesEnricher ( ) ;
17
+ builder . Services . AddMvc ( ) ;
18
+ builder . Services . AddOptions ( ) ;
19
+ builder . Services . AddHealthChecks ( )
20
+ . AddCheck ( "self" , ( ) => HealthCheckResult . Healthy ( ) ) ;
21
+ builder . Services
22
+ . AddHealthChecksUI ( )
23
+ . AddInMemoryStorage ( ) ;
24
+
25
+ var app = builder . Build ( ) ;
26
+
27
+ if ( app . Environment . IsDevelopment ( ) )
28
+ {
29
+ app . UseDeveloperExceptionPage ( ) ;
30
+ }
31
+ else
32
+ {
33
+ app . UseExceptionHandler ( "/Home/Error" ) ;
34
+ }
4
35
5
- try
6
- {
7
- Log . Information ( "Configuring web host ({ApplicationContext})..." , Program . AppName ) ;
8
- var host = BuildWebHost ( configuration , args ) ;
36
+ var pathBase = app . Configuration [ "PATH_BASE" ] ;
37
+ if ( ! string . IsNullOrEmpty ( pathBase ) )
38
+ {
39
+ app . UsePathBase ( pathBase ) ;
40
+ }
9
41
10
- LogPackagesVersionInfo ( ) ;
42
+ app . UseHealthChecksUI ( config =>
43
+ {
44
+ config . ResourcesPath = string . IsNullOrEmpty ( pathBase ) ? "/ui/resources" : $ "{ pathBase } /ui/resources";
45
+ config . UIPath = "/hc-ui" ;
46
+ } ) ;
11
47
12
- Log . Information ( "Starting web host ({ApplicationContext})..." , Program . AppName ) ;
13
- host . Run ( ) ;
48
+ app . UseStaticFiles ( ) ;
14
49
15
- return 0 ;
16
- }
17
- catch ( Exception ex )
18
- {
19
- Log . Fatal ( ex , "Program terminated unexpectedly ({ApplicationContext})!" , Program . AppName ) ;
20
- return 1 ;
50
+ app . UseRouting ( ) ;
51
+ app . MapDefaultControllerRoute ( ) ;
52
+ app . MapHealthChecks ( "/liveness" , new HealthCheckOptions
53
+ {
54
+ Predicate = r => r . Name . Contains ( "self" )
55
+ } ) ;
56
+
57
+ await app . RunAsync ( ) ;
21
58
}
22
- finally
59
+ catch ( Exception exception )
23
60
{
24
- Log . CloseAndFlush ( ) ;
61
+ Console . WriteLine ( exception ) ;
25
62
}
26
63
27
- IWebHost BuildWebHost ( IConfiguration configuration , string [ ] args ) =>
28
- WebHost . CreateDefaultBuilder ( args )
29
- . CaptureStartupErrors ( false )
30
- . ConfigureAppConfiguration ( x => x . AddConfiguration ( configuration ) )
31
- . UseStartup < Startup > ( )
32
- . UseContentRoot ( Directory . GetCurrentDirectory ( ) )
33
- . UseSerilog ( )
34
- . Build ( ) ;
35
-
36
- Serilog . ILogger CreateSerilogLogger ( IConfiguration configuration )
64
+ Serilog . ILogger CreateLogger ( IConfiguration configuration )
37
65
{
38
66
var seqServerUrl = configuration [ "Serilog:SeqServerUrl" ] ;
39
67
var logstashUrl = configuration [ "Serilog:LogstashgUrl" ] ;
@@ -48,63 +76,8 @@ Serilog.ILogger CreateSerilogLogger(IConfiguration configuration)
48
76
. CreateLogger ( ) ;
49
77
}
50
78
51
- IConfiguration GetConfiguration ( )
52
- {
53
- var builder = new ConfigurationBuilder ( )
54
- . SetBasePath ( Directory . GetCurrentDirectory ( ) )
55
- . AddJsonFile ( "appsettings.json" , optional : false , reloadOnChange : true )
56
- . AddEnvironmentVariables ( ) ;
57
-
58
- var config = builder . Build ( ) ;
59
-
60
- if ( config . GetValue < bool > ( "UseVault" , false ) )
61
- {
62
- TokenCredential credential = new ClientSecretCredential (
63
- config [ "Vault:TenantId" ] ,
64
- config [ "Vault:ClientId" ] ,
65
- config [ "Vault:ClientSecret" ] ) ;
66
- builder . AddAzureKeyVault ( new Uri ( $ "https://{ config [ "Vault:Name" ] } .vault.azure.net/") , credential ) ;
67
- }
68
-
69
- return builder . Build ( ) ;
70
- }
71
-
72
- string GetVersion ( Assembly assembly )
73
- {
74
- try
75
- {
76
- return $ "{ assembly . GetCustomAttribute < AssemblyFileVersionAttribute > ( ) ? . Version } ({ assembly . GetCustomAttribute < AssemblyInformationalVersionAttribute > ( ) ? . InformationalVersion . Split ( ) [ 0 ] } )";
77
- }
78
- catch
79
- {
80
- return string . Empty ;
81
- }
82
- }
83
-
84
- void LogPackagesVersionInfo ( )
85
- {
86
- var assemblies = new List < Assembly > ( ) ;
87
-
88
- foreach ( var dependencyName in typeof ( Program ) . Assembly . GetReferencedAssemblies ( ) )
89
- {
90
- try
91
- {
92
- // Try to load the referenced assembly...
93
- assemblies . Add ( Assembly . Load ( dependencyName ) ) ;
94
- }
95
- catch
96
- {
97
- // Failed to load assembly. Skip it.
98
- }
99
- }
100
-
101
- var versionList = assemblies . Select ( a => $ "-{ a . GetName ( ) . Name } - { GetVersion ( a ) } ") . OrderBy ( value => value ) ;
102
-
103
- Log . Logger . ForContext ( "PackageVersions" , string . Join ( "\n " , versionList ) ) . Information ( "Package versions ({ApplicationContext})" , Program . AppName ) ;
104
- }
105
-
106
79
public partial class Program
107
80
{
108
- private static readonly string _namespace = typeof ( Startup ) . Namespace ;
81
+ private static readonly string _namespace = typeof ( Program ) . Assembly . GetName ( ) . Name ;
109
82
public static readonly string AppName = _namespace ;
110
83
}
0 commit comments