Skip to content

Commit 0a295c5

Browse files
Add Serilog for structured logging and update configuration in appsettings (#108)
1 parent 16c3378 commit 0a295c5

File tree

5 files changed

+62
-12
lines changed

5 files changed

+62
-12
lines changed

OpenReferralApi/OpenReferralApi.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.15.0" />
2929
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.15.0" />
3030
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.15.0" />
31+
<PackageReference Include="Serilog.AspNetCore" Version="8.0.3" />
32+
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
3133
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.1.2" />
3234
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.15.0" />
3335
</ItemGroup>

OpenReferralApi/Program.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@
1515
using OpenTelemetry.Metrics;
1616
using OpenTelemetry.Resources;
1717
using OpenTelemetry.Trace;
18+
using Serilog;
1819

1920
var builder = WebApplication.CreateBuilder(args);
2021

2122
builder.Configuration.AddEnvironmentVariables("ORUK_API_");
2223

24+
// Configure Serilog
25+
builder.Host.UseSerilog((context, configuration) =>
26+
configuration.ReadFrom.Configuration(context.Configuration));
27+
2328
// Configure strongly-typed options
2429
builder.Services.Configure<SpecificationOptions>(
2530
builder.Configuration.GetSection(SpecificationOptions.SectionName));
@@ -311,6 +316,7 @@ await context.Response.WriteAsync(JsonSerializer.Serialize(new
311316
});
312317

313318
app.UseRouting();
319+
app.UseSerilogRequestLogging();
314320
app.UseCors();
315321
app.UseHttpsRedirection();
316322
app.UseResponseCaching();
@@ -320,3 +326,6 @@ await context.Response.WriteAsync(JsonSerializer.Serialize(new
320326
app.MapControllerRoute(name: "default", pattern: "{controller}/{action=Index}/{id?}");
321327

322328
app.Run();
329+
330+
// Ensure logs are flushed on shutdown
331+
Log.CloseAndFlush();

OpenReferralApi/appsettings.Production.json

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
{
2-
"Logging": {
3-
"LogLevel": {
2+
"Serilog": {
3+
"Using": ["Serilog.Sinks.Console"],
4+
"MinimumLevel": {
45
"Default": "Information",
5-
"Microsoft.AspNetCore": "Warning"
6-
}
6+
"Override": {
7+
"Microsoft.AspNetCore": "Warning",
8+
"System.Net.Http.HttpClient": "Warning"
9+
}
10+
},
11+
"WriteTo": [
12+
{
13+
"Name": "Console",
14+
"Args": {
15+
"outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u3}] {Message:lj} {Properties:j}{NewLine}{Exception}"
16+
}
17+
}
18+
],
19+
"Enrich": ["FromLogContext"]
720
},
821
"AllowedHosts": "*",
922
"Specification": {

OpenReferralApi/appsettings.Staging.json

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
{
2-
"Logging": {
3-
"LogLevel": {
2+
"Serilog": {
3+
"Using": ["Serilog.Sinks.Console"],
4+
"MinimumLevel": {
45
"Default": "Debug",
5-
"Microsoft.AspNetCore": "Information"
6-
}
6+
"Override": {
7+
"Microsoft.AspNetCore": "Information",
8+
"System.Net.Http.HttpClient": "Information"
9+
}
10+
},
11+
"WriteTo": [
12+
{
13+
"Name": "Console",
14+
"Args": {
15+
"outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u3}] {Message:lj} {Properties:j}{NewLine}{Exception}"
16+
}
17+
}
18+
],
19+
"Enrich": ["FromLogContext"]
720
},
821
"AllowedHosts": "*",
922
"Specification": {

OpenReferralApi/appsettings.json

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
{
2-
"Logging": {
3-
"LogLevel": {
2+
"Serilog": {
3+
"Using": ["Serilog.Sinks.Console"],
4+
"MinimumLevel": {
45
"Default": "Information",
5-
"Microsoft.AspNetCore": "Warning"
6-
}
6+
"Override": {
7+
"Microsoft.AspNetCore": "Warning",
8+
"System.Net.Http.HttpClient": "Warning"
9+
}
10+
},
11+
"WriteTo": [
12+
{
13+
"Name": "Console",
14+
"Args": {
15+
"outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u3}] {Message:lj} {Properties:j}{NewLine}{Exception}"
16+
}
17+
}
18+
],
19+
"Enrich": ["FromLogContext"]
720
},
821
"AllowedHosts": "*",
922
"Specification": {

0 commit comments

Comments
 (0)