1
1
using System ;
2
- using Microsoft . AspNetCore . Hosting ;
3
- using Microsoft . Extensions . Configuration ;
2
+ using System . Threading . Tasks ;
3
+ using Microsoft . AspNetCore . Builder ;
4
+ using Microsoft . Extensions . DependencyInjection ;
4
5
using Microsoft . Extensions . Hosting ;
5
6
using Serilog ;
6
7
using Serilog . Events ;
7
8
8
- namespace DynamicEntitySample . Web
9
+ namespace DynamicEntitySample . Web ;
10
+
11
+ public class Program
9
12
{
10
- public class Program
13
+ public async static Task < int > Main ( string [ ] args )
11
14
{
12
- public static int Main ( string [ ] args )
13
- {
14
- Log . Logger = new LoggerConfiguration ( )
15
+ Log . Logger = new LoggerConfiguration ( )
15
16
#if DEBUG
16
- . MinimumLevel . Debug ( )
17
+ . MinimumLevel . Debug ( )
17
18
#else
18
- . MinimumLevel . Information ( )
19
+ . MinimumLevel . Information ( )
19
20
#endif
20
- . MinimumLevel . Override ( "Microsoft" , LogEventLevel . Information )
21
- . MinimumLevel . Override ( "Microsoft.EntityFrameworkCore" , LogEventLevel . Warning )
22
- . Enrich . FromLogContext ( )
23
- . WriteTo . Async ( c => c . File ( "Logs/logs.txt" ) )
24
- #if DEBUG
25
- . WriteTo . Async ( c => c . Console ( ) )
26
- #endif
27
- . CreateLogger ( ) ;
21
+ . MinimumLevel . Override ( "Microsoft" , LogEventLevel . Information )
22
+ . MinimumLevel . Override ( "Microsoft.EntityFrameworkCore" , LogEventLevel . Warning )
23
+ . Enrich . FromLogContext ( )
24
+ . WriteTo . Async ( c => c . File ( "Logs/logs.txt" ) )
25
+ . WriteTo . Async ( c => c . Console ( ) )
26
+ . CreateLogger ( ) ;
28
27
29
- try
30
- {
31
- Log . Information ( "Starting web host." ) ;
32
- CreateHostBuilder ( args ) . Build ( ) . Run ( ) ;
33
- return 0 ;
34
- }
35
- catch ( Exception ex )
36
- {
37
- Log . Fatal ( ex , "Host terminated unexpectedly!" ) ;
38
- return 1 ;
39
- }
40
- finally
28
+ try
29
+ {
30
+ Log . Information ( "Starting web host." ) ;
31
+ var builder = WebApplication . CreateBuilder ( args ) ;
32
+ builder . Host . AddAppSettingsSecretsJson ( )
33
+ . UseAutofac ( )
34
+ . UseSerilog ( ) ;
35
+ await builder . AddApplicationAsync < DynamicEntitySampleWebModule > ( ) ;
36
+ var app = builder . Build ( ) ;
37
+ await app . InitializeApplicationAsync ( ) ;
38
+ await app . RunAsync ( ) ;
39
+ return 0 ;
40
+ }
41
+ catch ( Exception ex )
42
+ {
43
+ if ( ex is HostAbortedException )
41
44
{
42
- Log . CloseAndFlush ( ) ;
45
+ throw ;
43
46
}
44
- }
45
47
46
- internal static IHostBuilder CreateHostBuilder ( string [ ] args ) =>
47
- Host . CreateDefaultBuilder ( args )
48
- . ConfigureAppConfiguration ( build =>
49
- {
50
- build . AddJsonFile ( "appsettings.secrets.json" , optional : true ) ;
51
- } )
52
- . ConfigureWebHostDefaults ( webBuilder =>
53
- {
54
- webBuilder . UseStartup < Startup > ( ) ;
55
- } )
56
- . UseAutofac ( )
57
- . UseSerilog ( ) ;
48
+ Log . Fatal ( ex , "Host terminated unexpectedly!" ) ;
49
+ return 1 ;
50
+ }
51
+ finally
52
+ {
53
+ Log . CloseAndFlush ( ) ;
54
+ }
58
55
}
59
- }
56
+ }
0 commit comments