-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
76 lines (63 loc) · 2.71 KB
/
Program.cs
File metadata and controls
76 lines (63 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using ChannelsDVR_Log_Monitor.Models.Config;
using ChannelsDVR_Log_Monitor.Services.Bonjour;
using ChannelsDVR_Log_Monitor.Services.ChannelsDevices;
using ChannelsDVR_Log_Monitor.Services.ChannelsLogs;
using ChannelsDVR_Log_Monitor.Services.ChannelsUrl;
using ChannelsDVR_Log_Monitor.Services.Notifications;
using ChannelsDVR_Log_Monitor.Services.Persistence;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
using static ChannelsDVR_Log_Monitor.Models.Config.Logs;
namespace ChannelsDVR_Log_Monitor;
public class Program
{
public static async Task Main()
{
var services = new ServiceCollection();
ConfigureServices(services);
await using var serviceProvider = services.BuildServiceProvider();
Log.Information("Starting application");
using var cts = new CancellationTokenSource();
Console.CancelKeyPress += (sender, e) =>
{
e.Cancel = true; // Prevent the application from terminating immediately.
cts.Cancel();
};
var app = serviceProvider.GetRequiredService<App>();
await app.RunAsync(cts.Token);
Log.Information("Ending application");
Log.CloseAndFlush();
}
private static void ConfigureServices(IServiceCollection services)
{
var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddUserSecrets<Program>()
.AddEnvironmentVariables()
.Build();
Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(config).CreateLogger();
services.AddMemoryCache();
var appConfig = new AppConfig();
config.Bind(appConfig);
services.Configure<AppConfig>(config);
services.AddSingleton<IPersistenceService, LiteDbService>();
services.AddSingleton<INotificationService, EmailNotificationService>();
services.AddSingleton<NotificationHandler>();
services.AddSingleton<IBonjourService, ZeroconfService>();
services.AddSingleton<IChannelsUrlService, ChannelsUrlService>();
services.AddSingleton<IChannelsDevicesService, ChannelsDevicesService>();
switch (appConfig.Logs.MonitoringType)
{
case LogMonitoringType.File:
services.AddSingleton<IChannelsLogService, ChannelsLogFileService>();
break;
case LogMonitoringType.API:
services.AddHttpClient<IChannelsLogService, ChannelsLogHttpService>();
break;
default:
throw new InvalidOperationException("Invalid log monitoring type specified.");
}
services.AddSingleton<App>();
}
}