2017-08-29 11:40:23 +02:00
|
|
|
|
using Microsoft.AspNetCore;
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2017-10-11 16:26:44 +02:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2017-08-29 11:40:23 +02:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2018-12-13 12:13:59 +01:00
|
|
|
|
using Serilog;
|
2019-02-20 18:20:35 +00:00
|
|
|
|
using System;
|
2016-09-06 17:09:19 -07:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
2016-09-07 13:52:26 -07:00
|
|
|
|
namespace Microsoft.eShopOnContainers.WebMVC
|
2016-09-06 17:09:19 -07:00
|
|
|
|
{
|
|
|
|
|
public class Program
|
|
|
|
|
{
|
2019-02-26 21:55:04 +00:00
|
|
|
|
public static readonly string Namespace = typeof(Program).Namespace;
|
|
|
|
|
public static readonly string AppName = Namespace.Substring(Namespace.LastIndexOf('.', Namespace.LastIndexOf('.') - 1) + 1);
|
2019-02-20 18:20:35 +00:00
|
|
|
|
|
|
|
|
|
public static int Main(string[] args)
|
2016-09-06 17:09:19 -07:00
|
|
|
|
{
|
2019-02-20 18:20:35 +00:00
|
|
|
|
var configuration = GetConfiguration();
|
|
|
|
|
|
|
|
|
|
Log.Logger = CreateSerilogLogger(configuration);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2019-02-26 21:59:16 +00:00
|
|
|
|
Log.Information("Configuring web host ({ApplicationContext})...", AppName);
|
2019-02-20 18:20:35 +00:00
|
|
|
|
var host = BuildWebHost(configuration, args);
|
|
|
|
|
|
2019-02-26 21:59:16 +00:00
|
|
|
|
Log.Information("Starting web host ({ApplicationContext})...", AppName);
|
2019-02-20 18:20:35 +00:00
|
|
|
|
host.Run();
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2019-02-26 21:59:16 +00:00
|
|
|
|
Log.Fatal(ex, "Program terminated unexpectedly ({ApplicationContext})!", AppName);
|
2019-02-20 18:20:35 +00:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Log.CloseAndFlush();
|
|
|
|
|
}
|
2017-08-29 11:40:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-20 18:20:35 +00:00
|
|
|
|
private static IWebHost BuildWebHost(IConfiguration configuration, string[] args) =>
|
2017-08-29 11:40:23 +02:00
|
|
|
|
WebHost.CreateDefaultBuilder(args)
|
2020-12-23 15:19:36 +05:30
|
|
|
|
.CaptureStartupErrors(false)
|
2020-04-07 08:59:01 +02:00
|
|
|
|
.ConfigureAppConfiguration(x => x.AddConfiguration(configuration))
|
2016-09-06 17:09:19 -07:00
|
|
|
|
.UseStartup<Startup>()
|
2019-02-20 18:20:35 +00:00
|
|
|
|
.UseSerilog()
|
2017-10-11 16:26:44 +02:00
|
|
|
|
.Build();
|
2019-02-20 18:20:35 +00:00
|
|
|
|
|
2020-12-23 15:19:36 +05:30
|
|
|
|
private static Serilog.ILogger CreateSerilogLogger(IConfiguration configuration)
|
2019-02-20 18:20:35 +00:00
|
|
|
|
{
|
|
|
|
|
var seqServerUrl = configuration["Serilog:SeqServerUrl"];
|
2019-03-19 15:57:47 +01:00
|
|
|
|
var logstashUrl = configuration["Serilog:LogstashgUrl"];
|
2019-09-27 16:32:32 +02:00
|
|
|
|
var cfg = new LoggerConfiguration()
|
|
|
|
|
.ReadFrom.Configuration(configuration)
|
2019-02-26 21:55:04 +00:00
|
|
|
|
.Enrich.WithProperty("ApplicationContext", AppName)
|
2019-02-20 18:20:35 +00:00
|
|
|
|
.Enrich.FromLogContext()
|
2019-09-27 16:32:32 +02:00
|
|
|
|
.WriteTo.Console();
|
2020-12-23 15:19:36 +05:30
|
|
|
|
if (!string.IsNullOrWhiteSpace(seqServerUrl))
|
|
|
|
|
{
|
2019-09-27 16:32:32 +02:00
|
|
|
|
cfg.WriteTo.Seq(seqServerUrl);
|
|
|
|
|
}
|
2020-12-23 15:19:36 +05:30
|
|
|
|
if (!string.IsNullOrWhiteSpace(logstashUrl))
|
|
|
|
|
{
|
2019-09-27 16:32:32 +02:00
|
|
|
|
cfg.WriteTo.Http(logstashUrl);
|
|
|
|
|
}
|
|
|
|
|
return cfg.CreateLogger();
|
2019-02-20 18:20:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static IConfiguration GetConfiguration()
|
|
|
|
|
{
|
|
|
|
|
var builder = new ConfigurationBuilder()
|
|
|
|
|
.SetBasePath(Directory.GetCurrentDirectory())
|
|
|
|
|
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
|
|
|
|
.AddEnvironmentVariables();
|
|
|
|
|
|
|
|
|
|
return builder.Build();
|
|
|
|
|
}
|
2016-09-06 17:09:19 -07:00
|
|
|
|
}
|
2019-02-20 18:20:35 +00:00
|
|
|
|
}
|