top level statements
This commit is contained in:
parent
d8e64ab5bf
commit
4a71f483aa
@ -1,55 +1,29 @@
|
|||||||
using Microsoft.AspNetCore;
|
using Microsoft.AspNetCore;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
using System.IO;
|
|
||||||
|
|
||||||
namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator
|
|
||||||
{
|
|
||||||
public class Program
|
|
||||||
{
|
|
||||||
private static IConfiguration _configuration;
|
|
||||||
|
|
||||||
public static void Main(string[] args)
|
BuildWebHost(args).Run();
|
||||||
|
IWebHost BuildWebHost(string[] args) =>
|
||||||
|
WebHost
|
||||||
|
.CreateDefaultBuilder(args)
|
||||||
|
.ConfigureAppConfiguration(cb =>
|
||||||
{
|
{
|
||||||
_configuration = GetConfiguration();
|
var sources = cb.Sources;
|
||||||
|
sources.Insert(3, new Microsoft.Extensions.Configuration.Json.JsonConfigurationSource()
|
||||||
BuildWebHost(args).Run();
|
{
|
||||||
}
|
Optional = true,
|
||||||
|
Path = "appsettings.localhost.json",
|
||||||
public static IWebHost BuildWebHost(string[] args) =>
|
ReloadOnChange = false
|
||||||
WebHost
|
});
|
||||||
.CreateDefaultBuilder(args)
|
})
|
||||||
.ConfigureAppConfiguration(cb =>
|
.UseStartup<Startup>()
|
||||||
{
|
.UseSerilog((builderContext, config) =>
|
||||||
var sources = cb.Sources;
|
|
||||||
sources.Insert(3, new Microsoft.Extensions.Configuration.Json.JsonConfigurationSource()
|
|
||||||
{
|
|
||||||
Optional = true,
|
|
||||||
Path = "appsettings.localhost.json",
|
|
||||||
ReloadOnChange = false
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.UseStartup<Startup>()
|
|
||||||
.UseSerilog((builderContext, config) =>
|
|
||||||
{
|
|
||||||
config
|
|
||||||
.MinimumLevel.Information()
|
|
||||||
.Enrich.FromLogContext()
|
|
||||||
.WriteTo.Console();
|
|
||||||
})
|
|
||||||
.Build();
|
|
||||||
|
|
||||||
private static IConfiguration GetConfiguration()
|
|
||||||
{
|
{
|
||||||
var builder = new ConfigurationBuilder()
|
config
|
||||||
.SetBasePath(Directory.GetCurrentDirectory())
|
.MinimumLevel.Information()
|
||||||
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
.Enrich.FromLogContext()
|
||||||
.AddEnvironmentVariables();
|
.WriteTo.Console();
|
||||||
|
})
|
||||||
var config = builder.Build();
|
.Build();
|
||||||
|
|
||||||
return builder.Build();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,38 +1,29 @@
|
|||||||
using Microsoft.AspNetCore;
|
using Microsoft.AspNetCore;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
|
|
||||||
namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator
|
BuildWebHost(args).Run();
|
||||||
{
|
|
||||||
public class Program
|
IWebHost BuildWebHost(string[] args) =>
|
||||||
{
|
WebHost
|
||||||
public static void Main(string[] args)
|
.CreateDefaultBuilder(args)
|
||||||
|
.ConfigureAppConfiguration(cb =>
|
||||||
{
|
{
|
||||||
BuildWebHost(args).Run();
|
var sources = cb.Sources;
|
||||||
}
|
sources.Insert(3, new Microsoft.Extensions.Configuration.Json.JsonConfigurationSource()
|
||||||
|
{
|
||||||
public static IWebHost BuildWebHost(string[] args) =>
|
Optional = true,
|
||||||
WebHost
|
Path = "appsettings.localhost.json",
|
||||||
.CreateDefaultBuilder(args)
|
ReloadOnChange = false
|
||||||
.ConfigureAppConfiguration(cb =>
|
});
|
||||||
{
|
})
|
||||||
var sources = cb.Sources;
|
.UseStartup<Startup>()
|
||||||
sources.Insert(3, new Microsoft.Extensions.Configuration.Json.JsonConfigurationSource()
|
.UseSerilog((builderContext, config) =>
|
||||||
{
|
{
|
||||||
Optional = true,
|
config
|
||||||
Path = "appsettings.localhost.json",
|
.MinimumLevel.Information()
|
||||||
ReloadOnChange = false
|
.Enrich.FromLogContext()
|
||||||
});
|
.WriteTo.Console();
|
||||||
})
|
})
|
||||||
.UseStartup<Startup>()
|
.Build();
|
||||||
.UseSerilog((builderContext, config) =>
|
|
||||||
{
|
|
||||||
config
|
|
||||||
.MinimumLevel.Information()
|
|
||||||
.Enrich.FromLogContext()
|
|
||||||
.WriteTo.Console();
|
|
||||||
})
|
|
||||||
.Build();
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
@ -3,113 +3,109 @@ using Microsoft.AspNetCore;
|
|||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.Server.Kestrel.Core;
|
using Microsoft.AspNetCore.Server.Kestrel.Core;
|
||||||
|
using Microsoft.eShopOnContainers.Services.Basket.API;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
|
||||||
namespace Microsoft.eShopOnContainers.Services.Basket.API
|
var configuration = GetConfiguration();
|
||||||
|
|
||||||
|
Log.Logger = CreateSerilogLogger(configuration);
|
||||||
|
|
||||||
|
try
|
||||||
{
|
{
|
||||||
public class Program
|
Log.Information("Configuring web host ({ApplicationContext})...", Program.AppName);
|
||||||
{
|
var host = BuildWebHost(configuration, args);
|
||||||
public static readonly string Namespace = typeof(Program).Namespace;
|
|
||||||
public static readonly string AppName = Namespace.Substring(Namespace.LastIndexOf('.', Namespace.LastIndexOf('.') - 1) + 1);
|
|
||||||
|
|
||||||
public static int Main(string[] args)
|
Log.Information("Starting web host ({ApplicationContext})...", Program.AppName);
|
||||||
{
|
host.Run();
|
||||||
var configuration = GetConfiguration();
|
|
||||||
|
|
||||||
Log.Logger = CreateSerilogLogger(configuration);
|
return 0;
|
||||||
|
}
|
||||||
try
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Log.Information("Configuring web host ({ApplicationContext})...", AppName);
|
Log.Fatal(ex, "Program terminated unexpectedly ({ApplicationContext})!", Program.AppName);
|
||||||
var host = BuildWebHost(configuration, args);
|
return 1;
|
||||||
|
}
|
||||||
Log.Information("Starting web host ({ApplicationContext})...", AppName);
|
finally
|
||||||
host.Run();
|
{
|
||||||
|
Log.CloseAndFlush();
|
||||||
return 0;
|
}
|
||||||
}
|
|
||||||
catch (Exception ex)
|
IWebHost BuildWebHost(IConfiguration configuration, string[] args) =>
|
||||||
{
|
WebHost.CreateDefaultBuilder(args)
|
||||||
Log.Fatal(ex, "Program terminated unexpectedly ({ApplicationContext})!", AppName);
|
.CaptureStartupErrors(false)
|
||||||
return 1;
|
.ConfigureKestrel(options =>
|
||||||
}
|
{
|
||||||
finally
|
var ports = GetDefinedPorts(configuration);
|
||||||
{
|
options.Listen(IPAddress.Any, ports.httpPort, listenOptions =>
|
||||||
Log.CloseAndFlush();
|
{
|
||||||
}
|
listenOptions.Protocols = HttpProtocols.Http1AndHttp2;
|
||||||
}
|
});
|
||||||
|
|
||||||
private static IWebHost BuildWebHost(IConfiguration configuration, string[] args) =>
|
options.Listen(IPAddress.Any, ports.grpcPort, listenOptions =>
|
||||||
WebHost.CreateDefaultBuilder(args)
|
{
|
||||||
.CaptureStartupErrors(false)
|
listenOptions.Protocols = HttpProtocols.Http2;
|
||||||
.ConfigureKestrel(options =>
|
});
|
||||||
{
|
|
||||||
var ports = GetDefinedPorts(configuration);
|
})
|
||||||
options.Listen(IPAddress.Any, ports.httpPort, listenOptions =>
|
.ConfigureAppConfiguration(x => x.AddConfiguration(configuration))
|
||||||
{
|
.UseFailing(options =>
|
||||||
listenOptions.Protocols = HttpProtocols.Http1AndHttp2;
|
{
|
||||||
});
|
options.ConfigPath = "/Failing";
|
||||||
|
options.NotFilteredPaths.AddRange(new[] { "/hc", "/liveness" });
|
||||||
options.Listen(IPAddress.Any, ports.grpcPort, listenOptions =>
|
})
|
||||||
{
|
.UseStartup<Startup>()
|
||||||
listenOptions.Protocols = HttpProtocols.Http2;
|
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||||
});
|
.UseSerilog()
|
||||||
|
.Build();
|
||||||
})
|
|
||||||
.ConfigureAppConfiguration(x => x.AddConfiguration(configuration))
|
ILogger CreateSerilogLogger(IConfiguration configuration)
|
||||||
.UseFailing(options => {
|
{
|
||||||
options.ConfigPath = "/Failing";
|
var seqServerUrl = configuration["Serilog:SeqServerUrl"];
|
||||||
options.NotFilteredPaths.AddRange(new[] {"/hc","/liveness"});
|
var logstashUrl = configuration["Serilog:LogstashgUrl"];
|
||||||
})
|
return new LoggerConfiguration()
|
||||||
.UseStartup<Startup>()
|
.MinimumLevel.Verbose()
|
||||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
.Enrich.WithProperty("ApplicationContext", Program.AppName)
|
||||||
.UseSerilog()
|
.Enrich.FromLogContext()
|
||||||
.Build();
|
.WriteTo.Console()
|
||||||
|
.WriteTo.Seq(string.IsNullOrWhiteSpace(seqServerUrl) ? "http://seq" : seqServerUrl)
|
||||||
private static Serilog.ILogger CreateSerilogLogger(IConfiguration configuration)
|
.WriteTo.Http(string.IsNullOrWhiteSpace(logstashUrl) ? "http://logstash:8080" : logstashUrl)
|
||||||
{
|
.ReadFrom.Configuration(configuration)
|
||||||
var seqServerUrl = configuration["Serilog:SeqServerUrl"];
|
.CreateLogger();
|
||||||
var logstashUrl = configuration["Serilog:LogstashgUrl"];
|
}
|
||||||
return new LoggerConfiguration()
|
|
||||||
.MinimumLevel.Verbose()
|
IConfiguration GetConfiguration()
|
||||||
.Enrich.WithProperty("ApplicationContext", AppName)
|
{
|
||||||
.Enrich.FromLogContext()
|
var builder = new ConfigurationBuilder()
|
||||||
.WriteTo.Console()
|
.SetBasePath(Directory.GetCurrentDirectory())
|
||||||
.WriteTo.Seq(string.IsNullOrWhiteSpace(seqServerUrl) ? "http://seq" : seqServerUrl)
|
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
||||||
.WriteTo.Http(string.IsNullOrWhiteSpace(logstashUrl) ? "http://logstash:8080" : logstashUrl)
|
.AddEnvironmentVariables();
|
||||||
.ReadFrom.Configuration(configuration)
|
|
||||||
.CreateLogger();
|
var config = builder.Build();
|
||||||
}
|
|
||||||
|
if (config.GetValue<bool>("UseVault", false))
|
||||||
private static IConfiguration GetConfiguration()
|
{
|
||||||
{
|
builder.AddAzureKeyVault(
|
||||||
var builder = new ConfigurationBuilder()
|
$"https://{config["Vault:Name"]}.vault.azure.net/",
|
||||||
.SetBasePath(Directory.GetCurrentDirectory())
|
config["Vault:ClientId"],
|
||||||
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
config["Vault:ClientSecret"]);
|
||||||
.AddEnvironmentVariables();
|
}
|
||||||
|
|
||||||
var config = builder.Build();
|
return builder.Build();
|
||||||
|
}
|
||||||
if (config.GetValue<bool>("UseVault", false))
|
|
||||||
{
|
(int httpPort, int grpcPort) GetDefinedPorts(IConfiguration config)
|
||||||
builder.AddAzureKeyVault(
|
{
|
||||||
$"https://{config["Vault:Name"]}.vault.azure.net/",
|
var grpcPort = config.GetValue("GRPC_PORT", 5001);
|
||||||
config["Vault:ClientId"],
|
var port = config.GetValue("PORT", 80);
|
||||||
config["Vault:ClientSecret"]);
|
return (port, grpcPort);
|
||||||
}
|
}
|
||||||
|
|
||||||
return builder.Build();
|
public class Program {
|
||||||
}
|
|
||||||
|
public static string Namespace = typeof(Program).Namespace;
|
||||||
private static (int httpPort, int grpcPort) GetDefinedPorts(IConfiguration config)
|
public static string AppName = Namespace.Substring(Namespace.LastIndexOf('.', Namespace.LastIndexOf('.') - 1) + 1);
|
||||||
{
|
|
||||||
var grpcPort = config.GetValue("GRPC_PORT", 5001);
|
|
||||||
var port = config.GetValue("PORT", 80);
|
|
||||||
return (port, grpcPort);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user