-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
69 lines (61 loc) · 2.32 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
67
68
69
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
namespace SimpleHostingConsole
{
internal static class Program
{
private static IConfigurationBuilder BuildConfiguration(IConfigurationBuilder builder, string prefix, string[] args)
{
builder
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable(prefix + "ENVIRONMENT") ?? "Production"}.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables(prefix);
if (args != null)
{
builder.AddCommandLine(args);
}
return builder;
}
public static async Task Main(string[] args)
{
const string prefix = "HOSTING_";
var configuration = BuildConfiguration(new ConfigurationBuilder(), prefix, args).Build();
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
try
{
Log.Information("Starting up");
var builder = new HostBuilder()
.ConfigureAppConfiguration(configurationBuilder => BuildConfiguration(configurationBuilder, prefix, args))
.UseSerilog()
.ConfigureServices((hostContext, services) =>
{
services.AddOptions();
services.Configure<AppConfig>(hostContext.Configuration.GetSection("AppConfig"));
services.AddSingleton<IHostedService, PrintTextToConsoleService>();
});
await builder.RunConsoleAsync();
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly: {Exception}", ex.Message);
}
finally
{
Log.CloseAndFlush();
}
if (Debugger.IsAttached)
{
Debugger.Break();
}
}
}
}