Skip to content

Commit 286be68

Browse files
committed
Structured logging set up properly
Updated reference to Ardalis.SharedKernel
1 parent faaec28 commit 286be68

File tree

8 files changed

+64
-51
lines changed

8 files changed

+64
-51
lines changed

MinimalClean/Directory.Packages.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<PackageVersion Include="Ardalis.ListStartupServices" Version="1.1.4" />
66
<PackageVersion Include="Ardalis.Result" Version="10.1.0" />
77
<PackageVersion Include="Ardalis.Result.AspNetCore" Version="10.1.0" />
8-
<PackageVersion Include="Ardalis.SharedKernel" Version="4.1.0" />
8+
<PackageVersion Include="Ardalis.SharedKernel" Version="5.0.0" />
99
<PackageVersion Include="Ardalis.SmartEnum" Version="8.2.0" />
1010
<PackageVersion Include="Ardalis.Specification" Version="9.3.1" />
1111
<PackageVersion Include="Ardalis.Specification.EntityFrameworkCore" Version="9.3.1" />
Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,36 @@
1+
using System.Net.Sockets;
2+
13
var builder = DistributedApplication.CreateBuilder(args);
24

3-
// secret SQL password parameter
4-
var sqlPassword = builder.AddParameter("sql-password", "Your$tr0ngP@ss!", secret: true);
5+
// Add SQL Server container
6+
var sqlServer = builder.AddSqlServer("sqlserver")
7+
.WithLifetime(ContainerLifetime.Persistent);
58

6-
var sql = builder.AddSqlServer("sql", password: sqlPassword)
7-
.WithDataVolume()
8-
.WithLifetime(ContainerLifetime.Persistent);
9+
// Add the database
10+
var appDb = sqlServer.AddDatabase("AppDb");
911

10-
// define the app database
11-
var appDb = sql.AddDatabase("AppDb");
12+
// Papercut SMTP container for email testing
13+
var papercut = builder.AddContainer("papercut", "jijiechen/papercut", "latest")
14+
.WithEndpoint("smtp", e =>
15+
{
16+
e.TargetPort = 25; // container port
17+
e.Port = 25; // host port
18+
e.Protocol = ProtocolType.Tcp;
19+
e.UriScheme = "smtp";
20+
})
21+
.WithEndpoint("ui", e =>
22+
{
23+
e.TargetPort = 37408;
24+
e.Port = 37408;
25+
e.UriScheme = "http";
26+
});
1227

1328
// register the API project and link the DB
1429
builder.AddProject<Projects.MinimalClean_Architecture_Web>("web")
15-
.WithReference(appDb)
16-
.WaitFor(appDb)
17-
.WithUrl("https://localhost:57679"); // Match launchSettings
30+
.WithEnvironment("ASPNETCORE_ENVIRONMENT", builder.Environment.EnvironmentName)
31+
.WithReference(appDb)
32+
.WaitFor(papercut)
33+
.WaitFor(appDb)
34+
.WithUrl("https://localhost:57379"); // Match launchSettings
1835

1936
builder.Build().Run();

MinimalClean/src/MinimalClean.Architecture.Web/Configurations/LoggerConfigs.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
using Serilog;
1+
using Serilog;
22

33
namespace MinimalClean.Architecture.Web.Configurations;
44

55
public static class LoggerConfigs
66
{
77
public static WebApplicationBuilder AddLoggerConfigs(this WebApplicationBuilder builder)
88
{
9-
10-
builder.Host.UseSerilog((_, config) => config.ReadFrom.Configuration(builder.Configuration));
9+
// Add Serilog as an additional logging provider alongside OpenTelemetry
10+
// This allows both Serilog (for console/file) and OpenTelemetry (for Aspire) to work together
11+
builder.Logging.AddSerilog(new LoggerConfiguration()
12+
.ReadFrom.Configuration(builder.Configuration)
13+
.Enrich.FromLogContext()
14+
.Enrich.WithProperty("Application", builder.Environment.ApplicationName)
15+
.WriteTo.Console()
16+
.CreateLogger());
1117

1218
return builder;
1319
}

MinimalClean/src/MinimalClean.Architecture.Web/MinimalClean.Architecture.Web.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,15 @@
3333
<PackageReference Include="NimblePros.Metronome" />
3434
<PackageReference Include="Scalar.AspNetCore" />
3535
<PackageReference Include="Serilog.AspNetCore" />
36+
<PackageReference Include="Serilog.Sinks.OpenTelemetry" />
3637

3738
<PackageReference Include="Vogen">
3839
<IncludeAssets>compile; runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
3940
</PackageReference>
4041

4142
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" />
4243
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" />
43-
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" PrivateAssets="all" >
44+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" PrivateAssets="all">
4445
<IncludeAssets>runtime; build; native; analyzers; buildtransitive</IncludeAssets>
4546
</PackageReference>
4647

MinimalClean/src/MinimalClean.Architecture.Web/Program.cs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
1-
using FastEndpoints;
1+
using FastEndpoints;
22
using FastEndpoints.Swagger;
33
using MinimalClean.Architecture.Web.Configurations;
44
using Serilog;
55

66
var builder = WebApplication.CreateBuilder(args);
77

8-
builder.AddServiceDefaults();
8+
builder.AddServiceDefaults() // This sets up OpenTelemetry logging
9+
.AddLoggerConfigs(); // This adds Serilog for console formatting
910

10-
var logger = Serilog.Log.Logger = new Serilog.LoggerConfiguration()
11-
.Enrich.FromLogContext()
12-
.WriteTo.Console()
13-
.CreateLogger();
11+
using var loggerFactory = LoggerFactory.Create(config => config.AddConsole());
12+
var startupLogger = loggerFactory.CreateLogger<Program>();
1413

15-
logger.Information("Starting web host");
14+
startupLogger.LogInformation("Starting web host");
1615

17-
builder.AddLoggerConfigs();
18-
19-
var appLogger = new Serilog.Extensions.Logging.SerilogLoggerFactory(logger)
20-
.CreateLogger<Program>();
21-
22-
builder.Services.AddOptionConfigs(builder.Configuration, appLogger, builder);
23-
builder.Services.AddServiceConfigs(appLogger, builder);
16+
builder.Services.AddOptionConfigs(builder.Configuration, startupLogger, builder);
17+
builder.Services.AddServiceConfigs(startupLogger, builder);
2418

2519
builder.Services.AddFastEndpoints()
2620
.SwaggerDocument(o =>

MinimalClean/src/MinimalClean.Architecture.Web/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"environmentVariables": {
88
"ASPNETCORE_ENVIRONMENT": "Development"
99
},
10-
"applicationUrl": "https://localhost:57679/"
10+
"applicationUrl": "https://localhost:57379/"
1111
}
1212
}
1313
}

MinimalClean/src/MinimalClean.Architecture.Web/api.http

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@OrderDemo.Api_HostAddress = https://localhost:57679
1+
@OrderDemo.Api_HostAddress = https://localhost:57379
22

33
@OrderDemo.CartId = 68fe4cfd-84bb-4b8a-bb4e-528c26325c72
44
@OrderDemo.OrderId = ec5a03fe-fc8c-4659-9fc7-f46351e9bf6f

MinimalClean/src/MinimalClean.Architecture.Web/appsettings.json

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,26 @@
11
{
22
"ConnectionStrings": {
3-
"DefaultConnection": "Server=(localdb)\\v11.0;Database=cleanarchitecture;Trusted_Connection=True;MultipleActiveResultSets=true"
3+
"DefaultConnection": "Server=(localdb)\\v11.0;Database=cleanarchitecture;Trusted_Connection=True;MultipleActiveResultSets=true",
4+
"AppDb": "Server=(localdb)\\mssqllocaldb;Database=MinimalCleanArchitecture;Trusted_Connection=True;MultipleActiveResultSets=true"
45
},
56
"Serilog": {
67
"MinimumLevel": {
7-
"Default": "Information"
8+
"Default": "Information",
9+
"Override": {
10+
"Microsoft": "Warning",
11+
"Microsoft.Hosting.Lifetime": "Information",
12+
"Microsoft.AspNetCore": "Warning",
13+
"System": "Warning"
14+
}
815
},
16+
"Enrich": ["FromLogContext", "WithMachineName", "WithThreadId"],
917
"WriteTo": [
1018
{
11-
"Name": "Console"
12-
}//,
13-
//{
14-
// "Name": "File",
15-
// "Args": {
16-
// "path": "log.txt",
17-
// "rollingInterval": "Day"
18-
// }
19-
//}
20-
//Uncomment this section if you'd like to push your logs to Azure Application Insights
21-
//Full list of Serilog Sinks can be found here: https://github.com/serilog/serilog/wiki/Provided-Sinks
22-
//{
23-
// "Name": "ApplicationInsights",
24-
// "Args": {
25-
// "instrumentationKey": "", //Fill in with your ApplicationInsights InstrumentationKey
26-
// "telemetryConverter": "Serilog.Sinks.ApplicationInsights.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights"
27-
// }
28-
//}
19+
"Name": "Console",
20+
"Args": {
21+
"outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} {Properties:j}{NewLine}{Exception}"
22+
}
23+
}
2924
]
3025
},
3126
"Mailserver": {

0 commit comments

Comments
 (0)