/
Program.cs
66 lines (61 loc) · 2.75 KB
/
Program.cs
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
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Pitstop.Infrastructure.Messaging;
using Serilog;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace AuditlogService
{
class Program
{
public static async Task Main(string[] args)
{
var host = CreateHostBuilder(args).Build();
await host.RunAsync();
}
private static IHostBuilder CreateHostBuilder(string[] args)
{
var hostBuilder = Host.CreateDefaultBuilder(args)
.ConfigureHostConfiguration(configHost =>
{
configHost.SetBasePath(Directory.GetCurrentDirectory());
configHost.AddJsonFile("hostsettings.json", optional: true);
configHost.AddJsonFile($"appsettings.json", optional: false);
configHost.AddEnvironmentVariables();
configHost.AddEnvironmentVariables("PITSTOP_");
configHost.AddCommandLine(args);
})
.ConfigureAppConfiguration((hostContext, config) =>
{
config.AddJsonFile($"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", optional: false);
})
.ConfigureServices((hostContext, services) =>
{
services.AddTransient<IMessageHandler>((svc) =>
{
var rabbitMQConfigSection = hostContext.Configuration.GetSection("RabbitMQ");
string rabbitMQHost = rabbitMQConfigSection["Host"];
string rabbitMQUserName = rabbitMQConfigSection["UserName"];
string rabbitMQPassword = rabbitMQConfigSection["Password"];
return new RabbitMQMessageHandler(rabbitMQHost, rabbitMQUserName, rabbitMQPassword, "Pitstop", "Auditlog", ""); ;
});
services.AddTransient<AuditlogManagerConfig>((svc) =>
{
var auditlogConfigSection = hostContext.Configuration.GetSection("Auditlog");
string logPath = auditlogConfigSection["path"];
return new AuditlogManagerConfig { LogPath = logPath };
});
services.AddHostedService<AuditLogManager>();
})
.UseSerilog((hostContext, loggerConfiguration) =>
{
loggerConfiguration.ReadFrom.Configuration(hostContext.Configuration);
})
.UseConsoleLifetime();
return hostBuilder;
}
}
}