Skip to content

Commit 8de902c

Browse files
committed
Add Seq to Ordering.API
1 parent d83fa3b commit 8de902c

4 files changed

Lines changed: 94 additions & 56 deletions

File tree

src/Services/Ordering/Ordering.API/Ordering.API.csproj

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,33 @@
2727
</ItemGroup>
2828

2929
<ItemGroup>
30-
<PackageReference Include="AspNetCore.HealthChecks.SqlServer" Version="2.2.0" />
31-
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="2.2.0" />
3230
<PackageReference Include="AspNetCore.HealthChecks.AzureServiceBus" Version="2.2.0" />
3331
<PackageReference Include="AspNetCore.HealthChecks.Rabbitmq" Version="2.2.0" />
32+
<PackageReference Include="AspNetCore.HealthChecks.SqlServer" Version="2.2.0" />
33+
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="2.2.0" />
34+
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.2.1" />
35+
<PackageReference Include="Dapper" Version="1.50.4" />
3436
<PackageReference Include="FluentValidation.AspNetCore" Version="7.5.0" />
37+
<PackageReference Include="MediatR" Version="5.1.0" />
3538
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="5.1.0" />
36-
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.2.1" />
3739
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.2.1" />
3840
<PackageReference Include="Microsoft.ApplicationInsights.DependencyCollector" Version="2.6.1" />
3941
<PackageReference Include="Microsoft.ApplicationInsights.Kubernetes" Version="1.0.0-beta8" />
4042
<PackageReference Include="Microsoft.ApplicationInsights.ServiceFabric" Version="2.1.1-beta1" />
43+
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.2.0" />
4144
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.HealthChecks" Version="2.2.0" />
4245
<PackageReference Include="Microsoft.AspNetCore.HealthChecks" Version="1.0.0" />
4346
<PackageReference Include="Microsoft.Extensions.Configuration.AzureKeyVault" Version="2.2.0" />
4447
<PackageReference Include="Microsoft.Extensions.Logging.AzureAppServices" Version="2.2.0" />
45-
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.2.0" />
46-
<PackageReference Include="MediatR" Version="5.1.0" />
48+
<PackageReference Include="Polly" Version="6.0.1" />
4749
<PackageReference Include="Serilog.AspNetCore" Version="2.1.1" />
50+
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.1.2" />
51+
<PackageReference Include="Serilog.Settings.Configuration" Version="3.0.1" />
4852
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
53+
<PackageReference Include="Serilog.Sinks.Seq" Version="4.0.0" />
4954
<PackageReference Include="Swashbuckle.AspNetCore" Version="3.0.0" />
5055
<PackageReference Include="System.Reflection" Version="4.3.0" />
51-
<PackageReference Include="Dapper" Version="1.50.4" />
5256
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
53-
<PackageReference Include="Polly" Version="6.0.1" />
5457
</ItemGroup>
5558
<ItemGroup>
5659
<None Update="Setup\*">

src/Services/Ordering/Ordering.API/Program.cs

Lines changed: 70 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,22 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API
1515
{
1616
public class Program
1717
{
18-
public static void Main(string[] args)
18+
public static readonly string AppName = typeof(Program).Namespace;
19+
public static readonly string ShortAppName = AppName.Substring(AppName.LastIndexOf('.', AppName.LastIndexOf('.') - 1) + 1);
20+
21+
public static int Main(string[] args)
1922
{
20-
BuildWebHost(args)
21-
.MigrateDbContext<OrderingContext>((context, services) =>
23+
var configuration = GetConfiguration();
24+
25+
Log.Logger = CreateSerilogLogger(configuration);
26+
27+
try
28+
{
29+
Log.Information("Configuring web host ({Application})...", AppName);
30+
var host = BuildWebHost(configuration, args);
31+
32+
Log.Information("Applying migrations ({Application})...", AppName);
33+
host.MigrateDbContext<OrderingContext>((context, services) =>
2234
{
2335
var env = services.GetService<IHostingEnvironment>();
2436
var settings = services.GetService<IOptions<OrderingSettings>>();
@@ -28,46 +40,66 @@ public static void Main(string[] args)
2840
.SeedAsync(context, env, settings, logger)
2941
.Wait();
3042
})
31-
.MigrateDbContext<IntegrationEventLogContext>((_,__)=>{})
32-
.Run();
43+
.MigrateDbContext<IntegrationEventLogContext>((_, __) => { });
44+
45+
Log.Information("Starting web host ({Application})...", AppName);
46+
host.Run();
47+
48+
return 0;
49+
}
50+
catch (Exception ex)
51+
{
52+
Log.Fatal(ex, "Program terminated unexpectedly ({Application})!", AppName);
53+
return 1;
54+
}
55+
finally
56+
{
57+
Log.CloseAndFlush();
58+
}
3359
}
3460

35-
public static IWebHost BuildWebHost(string[] args) =>
36-
WebHost.CreateDefaultBuilder(args)
61+
private static IWebHost BuildWebHost(IConfiguration configuration, string[] args) =>
62+
WebHost.CreateDefaultBuilder(args)
63+
.CaptureStartupErrors(false)
3764
.UseStartup<Startup>()
65+
.UseApplicationInsights()
3866
.UseContentRoot(Directory.GetCurrentDirectory())
39-
.ConfigureAppConfiguration((builderContext, config) =>
40-
{
41-
var builtConfig = config.Build();
67+
.UseConfiguration(configuration)
68+
.UseSerilog()
69+
.Build();
4270

43-
var configurationBuilder = new ConfigurationBuilder();
44-
45-
if (Convert.ToBoolean(builtConfig["UseVault"]))
46-
{
47-
configurationBuilder.AddAzureKeyVault(
48-
$"https://{builtConfig["Vault:Name"]}.vault.azure.net/",
49-
builtConfig["Vault:ClientId"],
50-
builtConfig["Vault:ClientSecret"]);
51-
}
71+
private static Serilog.ILogger CreateSerilogLogger(IConfiguration configuration)
72+
{
73+
var seqServerUrl = configuration["Serilog:SeqServerUrl"];
5274

53-
configurationBuilder.AddEnvironmentVariables();
75+
return new LoggerConfiguration()
76+
.MinimumLevel.Verbose()
77+
.Enrich.WithProperty("Application", AppName)
78+
.Enrich.FromLogContext()
79+
.WriteTo.Console()
80+
.WriteTo.Seq(string.IsNullOrWhiteSpace(seqServerUrl) ? "http://seq" : seqServerUrl)
81+
.ReadFrom.Configuration(configuration)
82+
.CreateLogger();
83+
}
5484

55-
config.AddConfiguration(configurationBuilder.Build());
56-
})
57-
.ConfigureLogging((hostingContext, builder) =>
58-
{
59-
builder.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
60-
builder.AddConsole();
61-
builder.AddDebug();
62-
})
63-
.UseApplicationInsights()
64-
.UseSerilog((builderContext, config) =>
65-
{
66-
config
67-
.MinimumLevel.Information()
68-
.Enrich.FromLogContext()
69-
.WriteTo.Console();
70-
})
71-
.Build();
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();
91+
92+
var config = builder.Build();
93+
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
}
73-
}
105+
}

src/Services/Ordering/Ordering.API/Startup.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,15 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
7474

7575
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
7676
{
77-
loggerFactory.AddAzureWebAppDiagnostics();
78-
loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Trace);
77+
//loggerFactory.AddAzureWebAppDiagnostics();
78+
//loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Trace);
7979

8080
var pathBase = Configuration["PATH_BASE"];
8181
if (!string.IsNullOrEmpty(pathBase))
8282
{
8383
loggerFactory.CreateLogger("init").LogDebug($"Using PATH BASE '{pathBase}'");
8484
app.UsePathBase(pathBase);
85-
}
85+
}
8686

8787
app.UseCors("CorsPolicy");
8888

@@ -122,7 +122,7 @@ private void ConfigureEventBus(IApplicationBuilder app)
122122
eventBus.Subscribe<OrderStockConfirmedIntegrationEvent, IIntegrationEventHandler<OrderStockConfirmedIntegrationEvent>>();
123123
eventBus.Subscribe<OrderStockRejectedIntegrationEvent, IIntegrationEventHandler<OrderStockRejectedIntegrationEvent>>();
124124
eventBus.Subscribe<OrderPaymentFailedIntegrationEvent, IIntegrationEventHandler<OrderPaymentFailedIntegrationEvent>>();
125-
eventBus.Subscribe<OrderPaymentSuccededIntegrationEvent, IIntegrationEventHandler<OrderPaymentSuccededIntegrationEvent>>();
125+
eventBus.Subscribe<OrderPaymentSuccededIntegrationEvent, IIntegrationEventHandler<OrderPaymentSuccededIntegrationEvent>>();
126126
}
127127

128128

@@ -168,7 +168,7 @@ public static IServiceCollection AddCustomMvc(this IServiceCollection services)
168168
})
169169
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
170170
.AddControllersAsServices(); //Injecting Controllers themselves thru DI
171-
//For further info see: http://docs.autofac.org/en/latest/integration/aspnetcore.html#controllers-as-services
171+
//For further info see: http://docs.autofac.org/en/latest/integration/aspnetcore.html#controllers-as-services
172172

173173
services.AddCors(options =>
174174
{

src/Services/Ordering/Ordering.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;Database=Microsoft.eShopOnContainers.Services.OrderingDb;User Id=sa;Password=Pass@word;",
33
"IdentityUrl": "http://localhost:5105",
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,

0 commit comments

Comments
 (0)