Skip to content

Commit 4d6b2f0

Browse files
committed
Add Serilog and Seq working in Docker
1 parent e3daacb commit 4d6b2f0

7 files changed

Lines changed: 98 additions & 46 deletions

File tree

docker-compose.override.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ version: '3.4'
77
# 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.
88

99
services:
10+
seq:
11+
environment:
12+
- ACCEPT_EULA=Y
13+
ports:
14+
- "5340:80"
15+
1016
sql.data:
1117
environment:
1218
- SA_PASSWORD=Pass@word

docker-compose.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
version: '3.4'
22

33
services:
4+
seq:
5+
image: datalust/seq:latest
6+
47
sql.data:
58
image: microsoft/mssql-server-linux:2017-latest
69

src/Services/Catalog/Catalog.API/Catalog.API.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@
5151
<PackageReference Include="Microsoft.Extensions.Configuration.AzureKeyVault" Version="2.1.0" />
5252
<PackageReference Include="Microsoft.Extensions.Logging.AzureAppServices" Version="2.1.0" />
5353
<PackageReference Include="Serilog.AspNetCore" Version="2.1.1" />
54+
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.1.2" />
55+
<PackageReference Include="Serilog.Settings.Configuration" Version="3.0.1" />
5456
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
57+
<PackageReference Include="Serilog.Sinks.Seq" Version="4.0.0" />
5558
<PackageReference Include="Swashbuckle.AspNetCore" Version="3.0.0" />
5659
<PackageReference Include="System.IO.Compression.ZipFile" Version="4.3.0" />
5760
</ItemGroup>

src/Services/Catalog/Catalog.API/Program.cs

Lines changed: 69 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,65 +9,97 @@
99
using Serilog;
1010
using System;
1111
using System.IO;
12+
1213
namespace Microsoft.eShopOnContainers.Services.Catalog.API
1314
{
1415
public class Program
1516
{
16-
public static void Main(string[] args)
17+
private static readonly string ApplicationName = typeof(Program).Namespace;
18+
19+
public static int Main(string[] args)
1720
{
18-
BuildWebHost(args)
19-
.MigrateDbContext<CatalogContext>((context,services)=>
21+
var configuration = GetConfiguration();
22+
23+
Log.Logger = CreateSerilogLogger(configuration);
24+
25+
try
26+
{
27+
Log.Information("Configuring web host ({Application})...", ApplicationName);
28+
var host = BuildWebHost(configuration, args);
29+
30+
Log.Information("Applying migrations ({Application})...", ApplicationName);
31+
host.MigrateDbContext<CatalogContext>((context, services) =>
2032
{
2133
var env = services.GetService<IHostingEnvironment>();
2234
var settings = services.GetService<IOptions<CatalogSettings>>();
2335
var logger = services.GetService<ILogger<CatalogContextSeed>>();
2436

2537
new CatalogContextSeed()
26-
.SeedAsync(context,env,settings,logger)
38+
.SeedAsync(context, env, settings, logger)
2739
.Wait();
28-
2940
})
30-
.MigrateDbContext<IntegrationEventLogContext>((_,__)=> { })
31-
.Run();
41+
.MigrateDbContext<IntegrationEventLogContext>((_, __) => { });
42+
43+
Log.Information("Starting web host ({Application})...", ApplicationName);
44+
host.Run();
45+
46+
return 0;
47+
}
48+
catch (Exception ex)
49+
{
50+
Log.Fatal(ex, "Program terminated unexpectedly ({Application})!", ApplicationName);
51+
return 1;
52+
}
53+
finally
54+
{
55+
Log.CloseAndFlush();
56+
}
3257
}
3358

34-
public static IWebHost BuildWebHost(string[] args) =>
59+
private static IWebHost BuildWebHost(IConfiguration configuration, string[] args) =>
3560
WebHost.CreateDefaultBuilder(args)
36-
.UseStartup<Startup>()
61+
.CaptureStartupErrors(false)
62+
.UseStartup<Startup>()
3763
.UseApplicationInsights()
3864
.UseContentRoot(Directory.GetCurrentDirectory())
3965
.UseWebRoot("Pics")
40-
.ConfigureAppConfiguration((builderContext, config) =>
41-
{
42-
var builtConfig = config.Build();
66+
.UseConfiguration(configuration)
67+
.UseSerilog()
68+
.Build();
69+
70+
private static Serilog.ILogger CreateSerilogLogger(IConfiguration configuration)
71+
{
72+
var seqServerUrl = configuration["Serilog:SeqServerUrl"];
4373

44-
var configurationBuilder = new ConfigurationBuilder();
74+
return new LoggerConfiguration()
75+
.MinimumLevel.Verbose()
76+
.Enrich.WithMachineName()
77+
.Enrich.WithProperty("Application", ApplicationName)
78+
.Enrich.FromLogContext()
79+
.WriteTo.Console()
80+
.WriteTo.Seq(string.IsNullOrWhiteSpace(seqServerUrl) ? "http://seq" : seqServerUrl)
81+
.ReadFrom.Configuration(configuration)
82+
.CreateLogger();
83+
}
4584

46-
if (Convert.ToBoolean(builtConfig["UseVault"]))
47-
{
48-
configurationBuilder.AddAzureKeyVault(
49-
$"https://{builtConfig["Vault:Name"]}.vault.azure.net/",
50-
builtConfig["Vault:ClientId"],
51-
builtConfig["Vault:ClientSecret"]);
52-
}
85+
private static IConfiguration GetConfiguration()
86+
{
87+
var builder = new ConfigurationBuilder()
88+
.SetBasePath(Directory.GetCurrentDirectory())
89+
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
90+
.AddEnvironmentVariables();
5391

54-
configurationBuilder.AddEnvironmentVariables();
92+
var config = builder.Build();
5593

56-
config.AddConfiguration(configurationBuilder.Build());
57-
})
58-
.ConfigureLogging((hostingContext, builder) =>
59-
{
60-
builder.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
61-
builder.AddConsole();
62-
builder.AddDebug();
63-
})
64-
.UseSerilog((builderContext, config) =>
65-
{
66-
config
67-
.MinimumLevel.Information()
68-
.Enrich.FromLogContext()
69-
.WriteTo.Console();
70-
})
71-
.Build();
94+
if (config.GetValue<bool>("UseVault", false))
95+
{
96+
builder.AddAzureKeyVault(
97+
$"https://{config["Vault:Name"]}.vault.azure.net/",
98+
config["Vault:ClientId"],
99+
config["Vault:ClientSecret"]);
100+
}
101+
102+
return builder.Build();
103+
}
72104
}
73105
}

src/Services/Catalog/Catalog.API/Properties/launchSettings.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
"launchBrowser": true,
1414
"launchUrl": "/swagger",
1515
"environmentVariables": {
16-
"ASPNETCORE_ENVIRONMENT": "Development"
16+
"ConnectionString": "server=localhost,5433;Database=Microsoft.eShopOnContainers.Services.CatalogDb;User Id=sa;Password=Pass@word",
17+
"ASPNETCORE_ENVIRONMENT": "Development",
18+
"EventBusConnection": "localhost",
19+
"Serilog:SeqServerUrl": "http://locahost:5340"
1720
}
1821
},
1922
"Microsoft.eShopOnContainers.Services.Catalog.API": {

src/Services/Catalog/Catalog.API/appsettings.json

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
"ConnectionString": "Server=tcp:127.0.0.1,5433;Initial Catalog=Microsoft.eShopOnContainers.Services.CatalogDb;User Id=sa;Password=Pass@word",
33
"PicBaseUrl": "http://localhost:5101/api/v1/catalog/items/[0]/pic/",
44
"UseCustomizationData": false,
5-
"Logging": {
6-
"IncludeScopes": false,
7-
"LogLevel": {
8-
"Default": "Trace",
9-
"System": "Information",
10-
"Microsoft": "Information"
5+
"Serilog": {
6+
"SeqServerUrl": null,
7+
"MinimumLevel": {
8+
"Default": "Information",
9+
"Override": {
10+
"Microsoft": "Warning",
11+
"Microsoft.eShopOnContainers": "Information",
12+
"System": "Warning"
13+
}
1114
}
1215
},
1316
"AzureServiceBusEnabled": false,

src/Services/Catalog/Catalog.API/web.config

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
<configuration>
33
<system.webServer>
44
<handlers>
5-
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
5+
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
66
</handlers>
7-
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" forwardWindowsAuthToken="false" stdoutLogEnabled="false" />
7+
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" forwardWindowsAuthToken="false" stdoutLogEnabled="false">
8+
<environmentVariables />
9+
</aspNetCore>
810
</system.webServer>
911
</configuration>

0 commit comments

Comments
 (0)