-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
90 lines (80 loc) · 3.44 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using Genometric.TVQ.WebService.BuildingBlocks.WebHost;
using Genometric.TVQ.WebService.Infrastructure;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Serilog;
using System;
using System.IO;
namespace Genometric.TVQ.WebService
{
public sealed class Program
{
public const string AppName = "TVQ";
public const string APIPrefix = "api/v1/";
public static readonly string Namespace = typeof(Program).Namespace;
public static void Main(string[] args)
{
var configuration = GetConfiguration();
Log.Logger = CreateSerilogLogger(configuration);
try
{
var host = BuildWebHost(configuration, args);
host.MigrateDbContext<TVQContext>((context, services) =>
{
var env = services.GetService<IWebHostEnvironment>();
var settings = services.GetService<IOptions<Settings>>();
var logger = services.GetService<ILogger<TVQContextSeed>>();
TVQContextSeed.SeedAsync(context, env, settings, logger).Wait();
});
Log.Information("Starting web host ({ApplicationContext})...", AppName);
host.Run();
}
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception ex)
{
Log.Fatal(ex, "Program terminated unexpectedly ({ApplicationContext})!", AppName);
}
#pragma warning restore CA1031 // Do not catch general exception types
finally
{
Log.CloseAndFlush();
}
}
private static IWebHost BuildWebHost(IConfiguration configuration, string[] args) =>
WebHost.CreateDefaultBuilder(args)
.CaptureStartupErrors(false)
.UseStartup<Startup>()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseConfiguration(configuration)
.Build();
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
private static IConfiguration GetConfiguration()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();
return builder.Build();
}
private static Serilog.ILogger CreateSerilogLogger(IConfiguration configuration)
{
//var seqServerUrl = configuration["Serilog:SeqServerUrl"];
//var logstashUrl = configuration["Serilog:LogstashgUrl"];
return new LoggerConfiguration()
.MinimumLevel.Verbose()
.Enrich.WithProperty("ApplicationContext", AppName)
.Enrich.FromLogContext()
//.WriteTo.Console()
//.WriteTo.Seq(string.IsNullOrWhiteSpace(seqServerUrl) ? "http://seq" : seqServerUrl)
//.WriteTo.Http(string.IsNullOrWhiteSpace(logstashUrl) ? "http://logstash:8080" : logstashUrl)
//.ReadFrom.Configuration(configuration)
.CreateLogger();
}
}
}