2
2
using JixMinApi . Features . Todo ;
3
3
using JixMinApi . Shared ;
4
4
using Microsoft . OpenApi . Models ;
5
+ using Serilog ;
6
+ using Serilog . Formatting . Compact ;
5
7
using System . Reflection ;
6
8
7
- var builder = WebApplication . CreateBuilder ( args ) ;
9
+ Log . Logger = new LoggerConfiguration ( )
10
+ . WriteTo . Console ( )
11
+ . WriteTo . File ( new CompactJsonFormatter ( ) , "Logs/applog-.txt" , rollingInterval : RollingInterval . Day )
12
+ . CreateBootstrapLogger ( ) ;
8
13
9
- // Add services to the container.
10
- builder . Services . AddEndpointsApiExplorer ( ) ;
11
- builder . Services . AddSwaggerGen ( options =>
14
+ Log . Information ( "App Starting up ===================" ) ;
15
+
16
+ try
12
17
{
13
- options . SwaggerDoc ( "v1" , new OpenApiInfo
18
+ var builder = WebApplication . CreateBuilder ( args ) ;
19
+
20
+ // Add services to the container.
21
+ builder . Services . AddSerilog ( ( services , lc ) => lc
22
+ . ReadFrom . Configuration ( builder . Configuration )
23
+ . ReadFrom . Services ( services )
24
+ . Enrich . FromLogContext ( ) ) ;
25
+
26
+ builder . Services . AddEndpointsApiExplorer ( ) ;
27
+ builder . Services . AddSwaggerGen ( options =>
14
28
{
15
- Version = "v1" ,
16
- Title = "JixMinApi Demo" ,
17
- Description = "A simple minimal API Demo that uses vertical slice architecture." ,
29
+ options . SwaggerDoc ( "v1" , new OpenApiInfo
30
+ {
31
+ Version = "v1" ,
32
+ Title = "JixMinApi Demo" ,
33
+ Description = "A simple minimal API Demo that uses vertical slice architecture." ,
34
+ } ) ;
35
+
36
+ var xmlFile = $ "{ Assembly . GetExecutingAssembly ( ) . GetName ( ) . Name } .xml";
37
+ var xmlPath = Path . Combine ( AppContext . BaseDirectory , xmlFile ) ;
38
+ options . IncludeXmlComments ( xmlPath ) ;
18
39
} ) ;
19
40
20
- var xmlFile = $ "{ Assembly . GetExecutingAssembly ( ) . GetName ( ) . Name } .xml";
21
- var xmlPath = Path . Combine ( AppContext . BaseDirectory , xmlFile ) ;
22
- options . IncludeXmlComments ( xmlPath ) ;
23
- } ) ;
41
+ builder . Services . AddExceptionHandler < GlobalExceptionHandler > ( ) ;
42
+ builder . Services . AddProblemDetails ( ) ;
43
+
44
+ builder . Services . AddValidatorsFromAssembly ( typeof ( Program ) . Assembly ) ;
45
+ builder . Services . AddMediatR ( cfg => cfg . RegisterServicesFromAssembly ( typeof ( Program ) . Assembly ) ) ;
24
46
25
- builder . Services . AddExceptionHandler < GlobalExceptionHandler > ( ) ;
26
- builder . Services . AddProblemDetails ( ) ;
47
+ builder . Services . AddTodoEndpointServices ( ) ;
48
+ builder . Services . AddHealthChecks ( ) ;
27
49
28
- builder . Services . AddValidatorsFromAssembly ( typeof ( Program ) . Assembly ) ;
29
- builder . Services . AddMediatR ( cfg => cfg . RegisterServicesFromAssembly ( typeof ( Program ) . Assembly ) ) ;
50
+ var app = builder . Build ( ) ;
30
51
31
- builder . Services . AddTodoEndpointServices ( ) ;
32
- builder . Services . AddHealthChecks ( ) ;
52
+ // Configure the HTTP request pipeline.
53
+ if ( app . Environment . IsDevelopment ( ) )
54
+ {
55
+ app . UseSwagger ( ) ;
56
+ app . UseSwaggerUI ( ) ;
57
+ }
58
+
59
+ if ( ! app . Environment . IsDevelopment ( ) )
60
+ {
61
+ app . UseHttpsRedirection ( ) ;
62
+ app . UseExceptionHandler ( ) ;
63
+ }
33
64
34
- var app = builder . Build ( ) ;
65
+ app . UseSerilogRequestLogging ( ) ;
66
+ app . UseTodoEndpoints ( ) ;
67
+ app . MapHealthChecks ( "/healthz" ) ;
35
68
36
- // Configure the HTTP request pipeline.
37
- if ( app . Environment . IsDevelopment ( ) )
69
+ app . Run ( ) ;
70
+ }
71
+ catch ( Exception ex )
38
72
{
39
- app . UseSwagger ( ) ;
40
- app . UseSwaggerUI ( ) ;
73
+ Log . Fatal ( ex , "App terminated unexpectedly ===================" ) ;
41
74
}
42
-
43
- if ( ! app . Environment . IsDevelopment ( ) )
75
+ finally
44
76
{
45
- app . UseHttpsRedirection ( ) ;
46
- app . UseExceptionHandler ( ) ;
77
+ Log . CloseAndFlush ( ) ;
47
78
}
48
79
49
- app . UseTodoEndpoints ( ) ;
50
- app . MapHealthChecks ( "/healthz" ) ;
51
80
52
- app . Run ( ) ;
0 commit comments