2023-04-19 00:19:52 +05:30
|
|
|
|
var appName = "Ordering.BackgroundTasks";
|
|
|
|
|
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
|
|
|
|
|
{
|
|
|
|
|
Args = args,
|
|
|
|
|
ApplicationName = typeof(Program).Assembly.FullName
|
|
|
|
|
});
|
|
|
|
|
builder.Configuration.SetBasePath(Directory.GetCurrentDirectory());
|
|
|
|
|
builder.Configuration.AddJsonFile("appsettings.json", optional: true);
|
|
|
|
|
builder.Configuration.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", optional: true);
|
|
|
|
|
builder.Configuration.AddEnvironmentVariables();
|
|
|
|
|
builder.Host.UseSerilog(CreateSerilogLogger(builder.Configuration));
|
|
|
|
|
builder.Services.AddCustomHealthCheck(builder.Configuration)
|
|
|
|
|
.Configure<BackgroundTaskSettings>(builder.Configuration)
|
|
|
|
|
.AddOptions()
|
|
|
|
|
.AddHostedService<GracePeriodManagerService>()
|
|
|
|
|
.AddEventBus(builder.Configuration);
|
|
|
|
|
var app = builder.Build();
|
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
|
|
|
{
|
|
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
app.UseExceptionHandler("/Home/Error");
|
|
|
|
|
}
|
|
|
|
|
app.UseRouting();
|
2019-08-06 16:10:39 +02:00
|
|
|
|
|
2023-04-19 00:19:52 +05:30
|
|
|
|
app.MapHealthChecks("/hc", new HealthCheckOptions()
|
2019-08-06 16:10:39 +02:00
|
|
|
|
{
|
2023-04-19 00:19:52 +05:30
|
|
|
|
Predicate = _ => true,
|
|
|
|
|
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
|
|
|
|
|
});
|
|
|
|
|
app.MapHealthChecks("/liveness", new HealthCheckOptions
|
|
|
|
|
{
|
|
|
|
|
Predicate = r => r.Name.Contains("self")
|
|
|
|
|
});
|
2019-11-07 18:22:23 +00:00
|
|
|
|
|
2023-04-19 00:19:52 +05:30
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Log.Information("Starting web host ({ApplicationContext})...", Program.AppName);
|
|
|
|
|
await app.RunAsync();
|
2019-08-06 16:10:39 +02:00
|
|
|
|
|
2023-04-19 00:19:52 +05:30
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Log.Fatal(ex, "Program terminated unexpectedly ({ApplicationContext})!", Program.AppName);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Log.CloseAndFlush();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Serilog.ILogger CreateSerilogLogger(IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
var seqServerUrl = configuration["Serilog:SeqServerUrl"];
|
2023-04-27 21:54:22 +03:00
|
|
|
|
var logstashUrl = configuration["Serilog:LogstashUrl"];
|
2023-04-19 00:19:52 +05:30
|
|
|
|
return new LoggerConfiguration()
|
|
|
|
|
.MinimumLevel.Verbose()
|
|
|
|
|
.Enrich.WithProperty("ApplicationContext", Program.AppName)
|
|
|
|
|
.Enrich.FromLogContext()
|
|
|
|
|
.WriteTo.Console()
|
|
|
|
|
.WriteTo.Seq(string.IsNullOrWhiteSpace(seqServerUrl) ? "http://seq" : seqServerUrl)
|
|
|
|
|
.WriteTo.Http(string.IsNullOrWhiteSpace(logstashUrl) ? "http://logstash:8080" : logstashUrl, null)
|
|
|
|
|
.ReadFrom.Configuration(configuration)
|
|
|
|
|
.CreateLogger();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public partial class Program
|
|
|
|
|
{
|
|
|
|
|
public static string Namespace = typeof(Program).Assembly.GetName().Name;
|
|
|
|
|
public static string AppName = Namespace.Substring(Namespace.LastIndexOf('.', Namespace.LastIndexOf('.') - 1) + 1);
|
2019-08-06 16:10:39 +02:00
|
|
|
|
}
|