Add Serilog and Seq working in Docker
This commit is contained in:
parent
e3daacb9f6
commit
4d6b2f03d2
@ -7,6 +7,12 @@ version: '3.4'
|
||||
# An external IP or DNS name has to be used (instead localhost and the 10.0.75.1 IP) when testing the Web apps and the Xamarin apps from remote machines/devices using the same WiFi, for instance.
|
||||
|
||||
services:
|
||||
seq:
|
||||
environment:
|
||||
- ACCEPT_EULA=Y
|
||||
ports:
|
||||
- "5340:80"
|
||||
|
||||
sql.data:
|
||||
environment:
|
||||
- SA_PASSWORD=Pass@word
|
||||
|
@ -1,6 +1,9 @@
|
||||
version: '3.4'
|
||||
|
||||
services:
|
||||
seq:
|
||||
image: datalust/seq:latest
|
||||
|
||||
sql.data:
|
||||
image: microsoft/mssql-server-linux:2017-latest
|
||||
|
||||
|
@ -51,7 +51,10 @@
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.AzureKeyVault" Version="2.1.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.AzureAppServices" Version="2.1.0" />
|
||||
<PackageReference Include="Serilog.AspNetCore" Version="2.1.1" />
|
||||
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.1.2" />
|
||||
<PackageReference Include="Serilog.Settings.Configuration" Version="3.0.1" />
|
||||
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
|
||||
<PackageReference Include="Serilog.Sinks.Seq" Version="4.0.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="3.0.0" />
|
||||
<PackageReference Include="System.IO.Compression.ZipFile" Version="4.3.0" />
|
||||
</ItemGroup>
|
||||
|
@ -9,65 +9,97 @@ using Microsoft.Extensions.Options;
|
||||
using Serilog;
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace Microsoft.eShopOnContainers.Services.Catalog.API
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
private static readonly string ApplicationName = typeof(Program).Namespace;
|
||||
|
||||
public static int Main(string[] args)
|
||||
{
|
||||
BuildWebHost(args)
|
||||
.MigrateDbContext<CatalogContext>((context,services)=>
|
||||
var configuration = GetConfiguration();
|
||||
|
||||
Log.Logger = CreateSerilogLogger(configuration);
|
||||
|
||||
try
|
||||
{
|
||||
Log.Information("Configuring web host ({Application})...", ApplicationName);
|
||||
var host = BuildWebHost(configuration, args);
|
||||
|
||||
Log.Information("Applying migrations ({Application})...", ApplicationName);
|
||||
host.MigrateDbContext<CatalogContext>((context, services) =>
|
||||
{
|
||||
var env = services.GetService<IHostingEnvironment>();
|
||||
var settings = services.GetService<IOptions<CatalogSettings>>();
|
||||
var logger = services.GetService<ILogger<CatalogContextSeed>>();
|
||||
|
||||
new CatalogContextSeed()
|
||||
.SeedAsync(context,env,settings,logger)
|
||||
.SeedAsync(context, env, settings, logger)
|
||||
.Wait();
|
||||
|
||||
})
|
||||
.MigrateDbContext<IntegrationEventLogContext>((_,__)=> { })
|
||||
.Run();
|
||||
.MigrateDbContext<IntegrationEventLogContext>((_, __) => { });
|
||||
|
||||
Log.Information("Starting web host ({Application})...", ApplicationName);
|
||||
host.Run();
|
||||
|
||||
return 0;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Fatal(ex, "Program terminated unexpectedly ({Application})!", ApplicationName);
|
||||
return 1;
|
||||
}
|
||||
finally
|
||||
{
|
||||
Log.CloseAndFlush();
|
||||
}
|
||||
}
|
||||
|
||||
public static IWebHost BuildWebHost(string[] args) =>
|
||||
private static IWebHost BuildWebHost(IConfiguration configuration, string[] args) =>
|
||||
WebHost.CreateDefaultBuilder(args)
|
||||
.UseStartup<Startup>()
|
||||
.CaptureStartupErrors(false)
|
||||
.UseStartup<Startup>()
|
||||
.UseApplicationInsights()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseWebRoot("Pics")
|
||||
.ConfigureAppConfiguration((builderContext, config) =>
|
||||
{
|
||||
var builtConfig = config.Build();
|
||||
.UseConfiguration(configuration)
|
||||
.UseSerilog()
|
||||
.Build();
|
||||
|
||||
var configurationBuilder = new ConfigurationBuilder();
|
||||
private static Serilog.ILogger CreateSerilogLogger(IConfiguration configuration)
|
||||
{
|
||||
var seqServerUrl = configuration["Serilog:SeqServerUrl"];
|
||||
|
||||
if (Convert.ToBoolean(builtConfig["UseVault"]))
|
||||
{
|
||||
configurationBuilder.AddAzureKeyVault(
|
||||
$"https://{builtConfig["Vault:Name"]}.vault.azure.net/",
|
||||
builtConfig["Vault:ClientId"],
|
||||
builtConfig["Vault:ClientSecret"]);
|
||||
}
|
||||
return new LoggerConfiguration()
|
||||
.MinimumLevel.Verbose()
|
||||
.Enrich.WithMachineName()
|
||||
.Enrich.WithProperty("Application", ApplicationName)
|
||||
.Enrich.FromLogContext()
|
||||
.WriteTo.Console()
|
||||
.WriteTo.Seq(string.IsNullOrWhiteSpace(seqServerUrl) ? "http://seq" : seqServerUrl)
|
||||
.ReadFrom.Configuration(configuration)
|
||||
.CreateLogger();
|
||||
}
|
||||
|
||||
configurationBuilder.AddEnvironmentVariables();
|
||||
private static IConfiguration GetConfiguration()
|
||||
{
|
||||
var builder = new ConfigurationBuilder()
|
||||
.SetBasePath(Directory.GetCurrentDirectory())
|
||||
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
||||
.AddEnvironmentVariables();
|
||||
|
||||
config.AddConfiguration(configurationBuilder.Build());
|
||||
})
|
||||
.ConfigureLogging((hostingContext, builder) =>
|
||||
{
|
||||
builder.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
|
||||
builder.AddConsole();
|
||||
builder.AddDebug();
|
||||
})
|
||||
.UseSerilog((builderContext, config) =>
|
||||
{
|
||||
config
|
||||
.MinimumLevel.Information()
|
||||
.Enrich.FromLogContext()
|
||||
.WriteTo.Console();
|
||||
})
|
||||
.Build();
|
||||
var config = 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();
|
||||
}
|
||||
}
|
||||
}
|
@ -13,7 +13,10 @@
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "/swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
"ConnectionString": "server=localhost,5433;Database=Microsoft.eShopOnContainers.Services.CatalogDb;User Id=sa;Password=Pass@word",
|
||||
"ASPNETCORE_ENVIRONMENT": "Development",
|
||||
"EventBusConnection": "localhost",
|
||||
"Serilog:SeqServerUrl": "http://locahost:5340"
|
||||
}
|
||||
},
|
||||
"Microsoft.eShopOnContainers.Services.Catalog.API": {
|
||||
|
@ -2,12 +2,15 @@
|
||||
"ConnectionString": "Server=tcp:127.0.0.1,5433;Initial Catalog=Microsoft.eShopOnContainers.Services.CatalogDb;User Id=sa;Password=Pass@word",
|
||||
"PicBaseUrl": "http://localhost:5101/api/v1/catalog/items/[0]/pic/",
|
||||
"UseCustomizationData": false,
|
||||
"Logging": {
|
||||
"IncludeScopes": false,
|
||||
"LogLevel": {
|
||||
"Default": "Trace",
|
||||
"System": "Information",
|
||||
"Microsoft": "Information"
|
||||
"Serilog": {
|
||||
"SeqServerUrl": null,
|
||||
"MinimumLevel": {
|
||||
"Default": "Information",
|
||||
"Override": {
|
||||
"Microsoft": "Warning",
|
||||
"Microsoft.eShopOnContainers": "Information",
|
||||
"System": "Warning"
|
||||
}
|
||||
}
|
||||
},
|
||||
"AzureServiceBusEnabled": false,
|
||||
|
@ -2,8 +2,10 @@
|
||||
<configuration>
|
||||
<system.webServer>
|
||||
<handlers>
|
||||
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
|
||||
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
|
||||
</handlers>
|
||||
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" forwardWindowsAuthToken="false" stdoutLogEnabled="false" />
|
||||
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" forwardWindowsAuthToken="false" stdoutLogEnabled="false">
|
||||
<environmentVariables />
|
||||
</aspNetCore>
|
||||
</system.webServer>
|
||||
</configuration>
|
Loading…
x
Reference in New Issue
Block a user