|
1 |
| -using Autofac.Extensions.DependencyInjection; |
2 |
| -using Microsoft.AspNetCore.Hosting; |
3 |
| -using Microsoft.Extensions.Configuration; |
4 |
| -using Microsoft.Extensions.Hosting; |
5 |
| -using Ordering.BackgroundTasks.Extensions; |
6 |
| -using Serilog; |
7 |
| -using System.IO; |
| 1 | +var appName = "Ordering.BackgroundTasks"; |
| 2 | +var builder = WebApplication.CreateBuilder(new WebApplicationOptions |
| 3 | +{ |
| 4 | + Args = args, |
| 5 | + ApplicationName = typeof(Program).Assembly.FullName |
| 6 | +}); |
| 7 | +builder.Configuration.SetBasePath(Directory.GetCurrentDirectory()); |
| 8 | +builder.Configuration.AddJsonFile("appsettings.json", optional: true); |
| 9 | +builder.Configuration.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", optional: true); |
| 10 | +builder.Configuration.AddEnvironmentVariables(); |
| 11 | +builder.Host.UseSerilog(CreateSerilogLogger(builder.Configuration)); |
| 12 | +builder.Services.AddCustomHealthCheck(builder.Configuration) |
| 13 | + .Configure<BackgroundTaskSettings>(builder.Configuration) |
| 14 | + .AddOptions() |
| 15 | + .AddHostedService<GracePeriodManagerService>() |
| 16 | + .AddEventBus(builder.Configuration); |
| 17 | +var app = builder.Build(); |
| 18 | +if (app.Environment.IsDevelopment()) |
| 19 | +{ |
| 20 | + app.UseDeveloperExceptionPage(); |
| 21 | +} |
| 22 | +else |
| 23 | +{ |
| 24 | + app.UseExceptionHandler("/Home/Error"); |
| 25 | +} |
| 26 | +app.UseRouting(); |
8 | 27 |
|
9 |
| -namespace Ordering.BackgroundTasks |
| 28 | +app.MapHealthChecks("/hc", new HealthCheckOptions() |
10 | 29 | {
|
11 |
| - public class Program |
12 |
| - { |
13 |
| - public static readonly string AppName = typeof(Program).Assembly.GetName().Name; |
| 30 | + Predicate = _ => true, |
| 31 | + ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse |
| 32 | +}); |
| 33 | +app.MapHealthChecks("/liveness", new HealthCheckOptions |
| 34 | +{ |
| 35 | + Predicate = r => r.Name.Contains("self") |
| 36 | +}); |
14 | 37 |
|
15 |
| - public static void Main(string[] args) |
16 |
| - { |
17 |
| - CreateHostBuilder(args).Run(); |
18 |
| - } |
| 38 | +try |
| 39 | +{ |
| 40 | + Log.Information("Starting web host ({ApplicationContext})...", Program.AppName); |
| 41 | + await app.RunAsync(); |
19 | 42 |
|
20 |
| - public static IHost CreateHostBuilder(string[] args) => |
21 |
| - Host.CreateDefaultBuilder(args) |
22 |
| - .UseServiceProviderFactory(new AutofacServiceProviderFactory()) |
23 |
| - .ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>()) |
24 |
| - .ConfigureAppConfiguration((host, builder) => |
25 |
| - { |
26 |
| - builder.SetBasePath(Directory.GetCurrentDirectory()); |
27 |
| - builder.AddJsonFile("appsettings.json", optional: true); |
28 |
| - builder.AddJsonFile($"appsettings.{host.HostingEnvironment.EnvironmentName}.json", optional: true); |
29 |
| - builder.AddEnvironmentVariables(); |
30 |
| - builder.AddCommandLine(args); |
31 |
| - }) |
32 |
| - .ConfigureLogging((host, builder) => builder.UseSerilog(host.Configuration).AddSerilog()) |
33 |
| - .Build(); |
34 |
| - } |
| 43 | + return 0; |
| 44 | +} |
| 45 | +catch (Exception ex) |
| 46 | +{ |
| 47 | + Log.Fatal(ex, "Program terminated unexpectedly ({ApplicationContext})!", Program.AppName); |
| 48 | + return 1; |
| 49 | +} |
| 50 | +finally |
| 51 | +{ |
| 52 | + Log.CloseAndFlush(); |
| 53 | +} |
| 54 | + |
| 55 | +Serilog.ILogger CreateSerilogLogger(IConfiguration configuration) |
| 56 | +{ |
| 57 | + var seqServerUrl = configuration["Serilog:SeqServerUrl"]; |
| 58 | + var logstashUrl = configuration["Serilog:LogstashgUrl"]; |
| 59 | + return new LoggerConfiguration() |
| 60 | + .MinimumLevel.Verbose() |
| 61 | + .Enrich.WithProperty("ApplicationContext", Program.AppName) |
| 62 | + .Enrich.FromLogContext() |
| 63 | + .WriteTo.Console() |
| 64 | + .WriteTo.Seq(string.IsNullOrWhiteSpace(seqServerUrl) ? "http://seq" : seqServerUrl) |
| 65 | + .WriteTo.Http(string.IsNullOrWhiteSpace(logstashUrl) ? "http://logstash:8080" : logstashUrl, null) |
| 66 | + .ReadFrom.Configuration(configuration) |
| 67 | + .CreateLogger(); |
| 68 | +} |
| 69 | + |
| 70 | +public partial class Program |
| 71 | +{ |
| 72 | + public static string Namespace = typeof(Program).Assembly.GetName().Name; |
| 73 | + public static string AppName = Namespace.Substring(Namespace.LastIndexOf('.', Namespace.LastIndexOf('.') - 1) + 1); |
35 | 74 | }
|
0 commit comments