11
22using Microsoft . EntityFrameworkCore ;
3+ using Serilog ;
4+ using Serilog . Events ;
35using ThingConnect . Pulse . Server . Data ;
46using ThingConnect . Pulse . Server . Infrastructure ;
57using ThingConnect . Pulse . Server . Services ;
@@ -13,75 +15,110 @@ public class Program
1315{
1416 public static void Main ( string [ ] args )
1517 {
16- WebApplicationBuilder builder = WebApplication . CreateBuilder ( args ) ;
17-
18- // Add services to the container.
19- builder . Services . AddDbContext < PulseDbContext > ( options =>
20- options . UseSqlite ( builder . Configuration . GetConnectionString ( "DefaultConnection" ) ) ) ;
21-
22- // Add memory cache for settings service
23- builder . Services . AddMemoryCache ( ) ;
24-
25- // Add HTTP client for probes
26- builder . Services . AddHttpClient ( ) ;
27-
28- // Add configuration services
29- builder . Services . AddSingleton < ConfigParser > ( ) ;
30- builder . Services . AddScoped < IConfigurationService , ConfigurationService > ( ) ;
31- builder . Services . AddScoped < ISettingsService , SettingsService > ( ) ;
32-
33- // Add monitoring services
34- builder . Services . AddScoped < IProbeService , ProbeService > ( ) ;
35- builder . Services . AddScoped < IOutageDetectionService , OutageDetectionService > ( ) ;
36- builder . Services . AddScoped < IDiscoveryService , DiscoveryService > ( ) ;
37- builder . Services . AddScoped < IStatusService , StatusService > ( ) ;
38- builder . Services . AddScoped < IHistoryService , HistoryService > ( ) ;
39- builder . Services . AddHostedService < MonitoringBackgroundService > ( ) ;
40-
41- // Add rollup services
42- builder . Services . AddScoped < IRollupService , RollupService > ( ) ;
43- builder . Services . AddHostedService < RollupBackgroundService > ( ) ;
44-
45- // Add prune services
46- builder . Services . AddScoped < IPruneService , PruneService > ( ) ;
47-
48- builder . Services . AddControllers ( options =>
18+ // Configure Serilog for rolling file logging
19+ Log . Logger = new LoggerConfiguration ( )
20+ . MinimumLevel . Information ( )
21+ . MinimumLevel . Override ( "Microsoft" , LogEventLevel . Warning )
22+ . MinimumLevel . Override ( "Microsoft.Hosting.Lifetime" , LogEventLevel . Information )
23+ . Enrich . FromLogContext ( )
24+ . WriteTo . Console ( )
25+ . WriteTo . File (
26+ Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . CommonApplicationData ) ,
27+ "ThingConnect.Pulse" , "logs" , "pulse-.log" ) ,
28+ rollingInterval : RollingInterval . Day ,
29+ retainedFileCountLimit : 30 ,
30+ shared : true )
31+ . CreateLogger ( ) ;
32+
33+ try
4934 {
50- options . InputFormatters . Insert ( 0 , new PlainTextInputFormatter ( ) ) ;
51- } ) ;
52- // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
53- builder . Services . AddEndpointsApiExplorer ( ) ;
54- builder . Services . AddSwaggerGen ( ) ;
55-
56- WebApplication app = builder . Build ( ) ;
57-
58- // Initialize database with seed data in development
59- if ( app . Environment . IsDevelopment ( ) )
35+ Log . Information ( "Starting ThingConnect Pulse Server" ) ;
36+
37+ WebApplicationBuilder builder = WebApplication . CreateBuilder ( args ) ;
38+
39+ // Use Serilog as the logging provider
40+ builder . Host . UseSerilog ( ) ;
41+
42+ // Configure Windows Service hosting
43+ builder . Host . UseWindowsService ( ) ;
44+
45+ // Add services to the container.
46+ builder . Services . AddDbContext < PulseDbContext > ( options =>
47+ options . UseSqlite ( builder . Configuration . GetConnectionString ( "DefaultConnection" ) ) ) ;
48+
49+ // Add memory cache for settings service
50+ builder . Services . AddMemoryCache ( ) ;
51+
52+ // Add HTTP client for probes
53+ builder . Services . AddHttpClient ( ) ;
54+
55+ // Add configuration services
56+ builder . Services . AddSingleton < ConfigParser > ( ) ;
57+ builder . Services . AddScoped < IConfigurationService , ConfigurationService > ( ) ;
58+ builder . Services . AddScoped < ISettingsService , SettingsService > ( ) ;
59+
60+ // Add monitoring services
61+ builder . Services . AddScoped < IProbeService , ProbeService > ( ) ;
62+ builder . Services . AddScoped < IOutageDetectionService , OutageDetectionService > ( ) ;
63+ builder . Services . AddScoped < IDiscoveryService , DiscoveryService > ( ) ;
64+ builder . Services . AddScoped < IStatusService , StatusService > ( ) ;
65+ builder . Services . AddScoped < IHistoryService , HistoryService > ( ) ;
66+ builder . Services . AddHostedService < MonitoringBackgroundService > ( ) ;
67+
68+ // Add rollup services
69+ builder . Services . AddScoped < IRollupService , RollupService > ( ) ;
70+ builder . Services . AddHostedService < RollupBackgroundService > ( ) ;
71+
72+ // Add prune services
73+ builder . Services . AddScoped < IPruneService , PruneService > ( ) ;
74+
75+ builder . Services . AddControllers ( options =>
76+ {
77+ options . InputFormatters . Insert ( 0 , new PlainTextInputFormatter ( ) ) ;
78+ } ) ;
79+ // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
80+ builder . Services . AddEndpointsApiExplorer ( ) ;
81+ builder . Services . AddSwaggerGen ( ) ;
82+
83+ WebApplication app = builder . Build ( ) ;
84+
85+ // Initialize database with seed data in development
86+ if ( app . Environment . IsDevelopment ( ) )
87+ {
88+ using IServiceScope scope = app . Services . CreateScope ( ) ;
89+ PulseDbContext context = scope . ServiceProvider . GetRequiredService < PulseDbContext > ( ) ;
90+ SeedData . Initialize ( context ) ;
91+ }
92+
93+ app . UseDefaultFiles ( ) ;
94+ app . UseStaticFiles ( ) ;
95+
96+ // Configure the HTTP request pipeline.
97+ if ( app . Environment . IsDevelopment ( ) )
98+ {
99+ app . UseSwagger ( ) ;
100+ app . UseSwaggerUI ( ) ;
101+ }
102+
103+ app . UseHttpsRedirection ( ) ;
104+
105+ app . UseAuthorization ( ) ;
106+
107+ app . MapControllers ( ) ;
108+
109+ app . MapFallbackToFile ( "/index.html" ) ;
110+
111+ Log . Information ( "ThingConnect Pulse Server configured successfully" ) ;
112+ app . Run ( ) ;
113+ }
114+ catch ( Exception ex )
60115 {
61- using IServiceScope scope = app . Services . CreateScope ( ) ;
62- PulseDbContext context = scope . ServiceProvider . GetRequiredService < PulseDbContext > ( ) ;
63- SeedData . Initialize ( context ) ;
116+ Log . Fatal ( ex , "ThingConnect Pulse Server terminated unexpectedly" ) ;
64117 }
65-
66- app . UseDefaultFiles ( ) ;
67- app . UseStaticFiles ( ) ;
68-
69- // Configure the HTTP request pipeline.
70- if ( app . Environment . IsDevelopment ( ) )
118+ finally
71119 {
72- app . UseSwagger ( ) ;
73- app . UseSwaggerUI ( ) ;
120+ Log . Information ( "ThingConnect Pulse Server stopped" ) ;
121+ Log . CloseAndFlush ( ) ;
74122 }
75-
76- app . UseHttpsRedirection ( ) ;
77-
78- app . UseAuthorization ( ) ;
79-
80-
81- app . MapControllers ( ) ;
82-
83- app . MapFallbackToFile ( "/index.html" ) ;
84-
85- app . Run ( ) ;
86123 }
87124}
0 commit comments