|
@ -3,86 +3,83 @@ using Microsoft.AspNetCore.Builder; |
|
|
using Microsoft.AspNetCore.Hosting; |
|
|
using Microsoft.AspNetCore.Hosting; |
|
|
using Microsoft.Extensions.Configuration; |
|
|
using Microsoft.Extensions.Configuration; |
|
|
using Microsoft.Extensions.Logging; |
|
|
using Microsoft.Extensions.Logging; |
|
|
|
|
|
using Payment.API; |
|
|
using Serilog; |
|
|
using Serilog; |
|
|
using System; |
|
|
using System; |
|
|
using System.IO; |
|
|
using System.IO; |
|
|
|
|
|
|
|
|
namespace Payment.API |
|
|
|
|
|
{ |
|
|
|
|
|
public class Program |
|
|
|
|
|
{ |
|
|
|
|
|
public static readonly string Namespace = typeof(Program).Namespace; |
|
|
|
|
|
public static readonly string AppName = Namespace; |
|
|
|
|
|
|
|
|
|
|
|
public static int Main(string[] args) |
|
|
|
|
|
{ |
|
|
|
|
|
var configuration = GetConfiguration(); |
|
|
|
|
|
|
|
|
var configuration = GetConfiguration(); |
|
|
|
|
|
|
|
|
Log.Logger = CreateSerilogLogger(configuration); |
|
|
|
|
|
|
|
|
Log.Logger = CreateSerilogLogger(configuration); |
|
|
|
|
|
|
|
|
try |
|
|
|
|
|
{ |
|
|
|
|
|
Log.Information("Configuring web host ({ApplicationContext})...", AppName); |
|
|
|
|
|
var host = BuildWebHost(configuration, args); |
|
|
|
|
|
|
|
|
try |
|
|
|
|
|
{ |
|
|
|
|
|
Log.Information("Configuring web host ({ApplicationContext})...", Program.AppName); |
|
|
|
|
|
var host = BuildWebHost(configuration, args); |
|
|
|
|
|
|
|
|
Log.Information("Starting web host ({ApplicationContext})...", AppName); |
|
|
|
|
|
host.Run(); |
|
|
|
|
|
|
|
|
Log.Information("Starting web host ({ApplicationContext})...", Program.AppName); |
|
|
|
|
|
host.Run(); |
|
|
|
|
|
|
|
|
return 0; |
|
|
|
|
|
} |
|
|
|
|
|
catch (Exception ex) |
|
|
|
|
|
{ |
|
|
|
|
|
Log.Fatal(ex, "Program terminated unexpectedly ({ApplicationContext})!", AppName); |
|
|
|
|
|
return 1; |
|
|
|
|
|
} |
|
|
|
|
|
finally |
|
|
|
|
|
{ |
|
|
|
|
|
Log.CloseAndFlush(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return 0; |
|
|
|
|
|
} |
|
|
|
|
|
catch (Exception ex) |
|
|
|
|
|
{ |
|
|
|
|
|
Log.Fatal(ex, "Program terminated unexpectedly ({ApplicationContext})!", Program.AppName); |
|
|
|
|
|
return 1; |
|
|
|
|
|
} |
|
|
|
|
|
finally |
|
|
|
|
|
{ |
|
|
|
|
|
Log.CloseAndFlush(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
private static IWebHost BuildWebHost(IConfiguration configuration, string[] args) => |
|
|
|
|
|
WebHost.CreateDefaultBuilder(args) |
|
|
|
|
|
.CaptureStartupErrors(false) |
|
|
|
|
|
.ConfigureAppConfiguration(x => x.AddConfiguration(configuration)) |
|
|
|
|
|
.UseStartup<Startup>() |
|
|
|
|
|
.UseContentRoot(Directory.GetCurrentDirectory()) |
|
|
|
|
|
.UseSerilog() |
|
|
|
|
|
.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(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
IWebHost BuildWebHost(IConfiguration configuration, string[] args) => |
|
|
|
|
|
WebHost.CreateDefaultBuilder(args) |
|
|
|
|
|
.CaptureStartupErrors(false) |
|
|
|
|
|
.ConfigureAppConfiguration(x => x.AddConfiguration(configuration)) |
|
|
|
|
|
.UseStartup<Startup>() |
|
|
|
|
|
.UseContentRoot(Directory.GetCurrentDirectory()) |
|
|
|
|
|
.UseSerilog() |
|
|
|
|
|
.Build(); |
|
|
|
|
|
|
|
|
private static IConfiguration GetConfiguration() |
|
|
|
|
|
{ |
|
|
|
|
|
var builder = new ConfigurationBuilder() |
|
|
|
|
|
.SetBasePath(Directory.GetCurrentDirectory()) |
|
|
|
|
|
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) |
|
|
|
|
|
.AddEnvironmentVariables(); |
|
|
|
|
|
|
|
|
Serilog.ILogger CreateSerilogLogger(IConfiguration configuration) |
|
|
|
|
|
{ |
|
|
|
|
|
var seqServerUrl = configuration["Serilog:SeqServerUrl"]; |
|
|
|
|
|
var logstashUrl = configuration["Serilog:LogstashgUrl"]; |
|
|
|
|
|
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) |
|
|
|
|
|
.ReadFrom.Configuration(configuration) |
|
|
|
|
|
.CreateLogger(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
var config = builder.Build(); |
|
|
|
|
|
|
|
|
IConfiguration GetConfiguration() |
|
|
|
|
|
{ |
|
|
|
|
|
var builder = new ConfigurationBuilder() |
|
|
|
|
|
.SetBasePath(Directory.GetCurrentDirectory()) |
|
|
|
|
|
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) |
|
|
|
|
|
.AddEnvironmentVariables(); |
|
|
|
|
|
|
|
|
if (config.GetValue<bool>("UseVault", false)) |
|
|
|
|
|
{ |
|
|
|
|
|
builder.AddAzureKeyVault( |
|
|
|
|
|
$"https://{config["Vault:Name"]}.vault.azure.net/", |
|
|
|
|
|
config["Vault:ClientId"], |
|
|
|
|
|
config["Vault:ClientSecret"]); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
var config = builder.Build(); |
|
|
|
|
|
|
|
|
return builder.Build(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if (config.GetValue<bool>("UseVault", false)) |
|
|
|
|
|
{ |
|
|
|
|
|
builder.AddAzureKeyVault( |
|
|
|
|
|
$"https://{config["Vault:Name"]}.vault.azure.net/", |
|
|
|
|
|
config["Vault:ClientId"], |
|
|
|
|
|
config["Vault:ClientSecret"]); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return builder.Build(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public class Program |
|
|
|
|
|
{ |
|
|
|
|
|
public static string Namespace = typeof(Startup).Namespace; |
|
|
|
|
|
public static string AppName = Namespace.Substring(Namespace.LastIndexOf('.', Namespace.LastIndexOf('.') - 1) + 1); |
|
|
} |
|
|
} |