Skip to content

Commit

Permalink
move structured logging into aspire (#1745)
Browse files Browse the repository at this point in the history
* move structured logging into aspire

* don't write output outside of test host
  • Loading branch information
Erwinvandervalk authored Feb 4, 2025
1 parent 9d70b0b commit 68ef1f8
Show file tree
Hide file tree
Showing 35 changed files with 156 additions and 336 deletions.
1 change: 0 additions & 1 deletion bff/samples/Apis/Api.DPoP/Api.DPoP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<ItemGroup>
<PackageReference Include="Duende.IdentityModel" />
<PackageReference Include="Duende.AspNetCore.Authentication.JwtBearer" />
<PackageReference Include="Serilog.AspNetCore" />
</ItemGroup>

<ItemGroup>
Expand Down
3 changes: 1 addition & 2 deletions bff/samples/Apis/Api.DPoP/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.IdentityModel.Tokens;
using Serilog;

namespace Api.DPoP;

Expand Down Expand Up @@ -68,7 +67,7 @@ public static WebApplication ConfigurePipeline(this WebApplication app)
// ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto | ForwardedHeaders.XForwardedHost,
// });

app.UseSerilogRequestLogging();
app.UseHttpLogging();

if (app.Environment.IsDevelopment())
{
Expand Down
42 changes: 6 additions & 36 deletions bff/samples/Apis/Api.DPoP/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,14 @@
using Api.DPoP;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Events;

Console.Title = "DPoP Api";

Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.CreateBootstrapLogger();
var builder = WebApplication.CreateBuilder(args);
builder.AddServiceDefaults();

Log.Information("Starting up");
var app = builder
.ConfigureServices()
.ConfigurePipeline();

try
{
var builder = WebApplication.CreateBuilder(args);
builder.AddServiceDefaults();

builder.Host.UseSerilog((ctx, lc) => lc
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}")
.Enrich.FromLogContext()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Information)
.MinimumLevel.Override("System", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.AspNetCore.Authentication", LogEventLevel.Information)
.ReadFrom.Configuration(ctx.Configuration));

var app = builder
.ConfigureServices()
.ConfigurePipeline();

app.Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "Unhandled exception");
}
finally
{
Log.Information("Shut down complete");
Log.CloseAndFlush();
}
app.Run();
1 change: 0 additions & 1 deletion bff/samples/Apis/Api.Isolated/Api.Isolated.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<ItemGroup>
<PackageReference Include="Duende.IdentityModel" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" />
<PackageReference Include="Serilog.AspNetCore" />
</ItemGroup>

<ItemGroup>
Expand Down
3 changes: 1 addition & 2 deletions bff/samples/Apis/Api.Isolated/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.IdentityModel.Tokens;
using Serilog;

namespace Api.Isolated
{
Expand Down Expand Up @@ -56,7 +55,7 @@ public static WebApplication ConfigurePipeline(this WebApplication app)
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto | ForwardedHeaders.XForwardedHost,
});

app.UseSerilogRequestLogging();
app.UseHttpLogging();

if (app.Environment.IsDevelopment())
{
Expand Down
42 changes: 6 additions & 36 deletions bff/samples/Apis/Api.Isolated/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,14 @@
using Api.Isolated;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Events;

Console.Title = "Isolated Api";

Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.CreateBootstrapLogger();
var builder = WebApplication.CreateBuilder(args);
builder.AddServiceDefaults();

Log.Information("Starting up");
var app = builder
.ConfigureServices()
.ConfigurePipeline();

try
{
var builder = WebApplication.CreateBuilder(args);
builder.AddServiceDefaults();

builder.Host.UseSerilog((ctx, lc) => lc
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}")
.Enrich.FromLogContext()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Information)
.MinimumLevel.Override("System", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.AspNetCore.Authentication", LogEventLevel.Information)
.ReadFrom.Configuration(ctx.Configuration));

var app = builder
.ConfigureServices()
.ConfigurePipeline();

app.Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "Unhandled exception");
}
finally
{
Log.Information("Shut down complete");
Log.CloseAndFlush();
}
app.Run();
3 changes: 1 addition & 2 deletions bff/samples/Apis/Api/Api.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFrameworks>net9.0</TargetFrameworks>
Expand All @@ -7,7 +7,6 @@
<ItemGroup>
<PackageReference Include="Duende.IdentityModel" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" />
<PackageReference Include="Serilog.AspNetCore" />
</ItemGroup>

<ItemGroup>
Expand Down
3 changes: 1 addition & 2 deletions bff/samples/Apis/Api/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.IdentityModel.Tokens;
using Serilog;

namespace Api;

Expand Down Expand Up @@ -57,7 +56,7 @@ public static WebApplication ConfigurePipeline(this WebApplication app)
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto | ForwardedHeaders.XForwardedHost,
});

app.UseSerilogRequestLogging();
app.UseHttpLogging();

if (app.Environment.IsDevelopment())
{
Expand Down
45 changes: 6 additions & 39 deletions bff/samples/Apis/Api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,45 +1,12 @@
using System;
using Api;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Events;
var builder = WebApplication.CreateBuilder(args);

Console.Title = "Api";
builder.AddServiceDefaults();

Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.CreateBootstrapLogger();
var app = builder
.ConfigureServices()
.ConfigurePipeline();

Log.Information("Starting up");

try
{
var builder = WebApplication.CreateBuilder(args);
builder.AddServiceDefaults();

builder.Host.UseSerilog((ctx, lc) => lc
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}")
.Enrich.FromLogContext()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Information)
.MinimumLevel.Override("System", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.AspNetCore.Authentication", LogEventLevel.Information)
.ReadFrom.Configuration(ctx.Configuration));

var app = builder
.ConfigureServices()
.ConfigurePipeline();

app.Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "Unhandled exception");
}
finally
{
Log.Information("Shut down complete");
Log.CloseAndFlush();
}
app.Run();
3 changes: 1 addition & 2 deletions bff/samples/Bff.DPoP/Bff.DPoP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" />
<PackageReference Include="Serilog.AspNetCore" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect"/>
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 1 addition & 3 deletions bff/samples/Bff.DPoP/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
using Serilog;
using Yarp.ReverseProxy.Configuration;


namespace Bff.DPoP;

internal static class Extensions
Expand Down Expand Up @@ -152,7 +150,7 @@ public static WebApplication ConfigureServices(this WebApplicationBuilder builde

public static WebApplication ConfigurePipeline(this WebApplication app)
{
app.UseSerilogRequestLogging();
app.UseHttpLogging();
app.UseDeveloperExceptionPage();

app.UseDefaultFiles();
Expand Down
42 changes: 6 additions & 36 deletions bff/samples/Bff.DPoP/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,14 @@
using Bff.DPoP;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Events;

Console.Title = "BFF.DPoP";

Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.CreateBootstrapLogger();
var builder = WebApplication.CreateBuilder(args);
builder.AddServiceDefaults();

Log.Information("Starting up");
var app = builder
.ConfigureServices()
.ConfigurePipeline();

try
{
var builder = WebApplication.CreateBuilder(args);
builder.AddServiceDefaults();

builder.Host.UseSerilog((ctx, lc) => lc
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}")
.Enrich.FromLogContext()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Information)
.MinimumLevel.Override("System", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.AspNetCore.Authentication", LogEventLevel.Information)
.ReadFrom.Configuration(ctx.Configuration));

var app = builder
.ConfigureServices()
.ConfigurePipeline();

app.Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "Unhandled exception");
}
finally
{
Log.Information("Shut down complete");
Log.CloseAndFlush();
}
app.Run();
1 change: 0 additions & 1 deletion bff/samples/Bff.EF/Bff.EF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" />
<PackageReference Include="Serilog.AspNetCore" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 1 addition & 3 deletions bff/samples/Bff.EF/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
using UserSessionDb.Migrations.UserSessions;

namespace Bff.EF
{
internal static class Extensions
{

public static WebApplication ConfigureServices(this WebApplicationBuilder builder)
{
var services = builder.Services;
Expand Down Expand Up @@ -79,7 +77,7 @@ public static WebApplication ConfigureServices(this WebApplicationBuilder builde

public static WebApplication ConfigurePipeline(this WebApplication app)
{
app.UseSerilogRequestLogging();
app.UseHttpLogging();
app.UseDeveloperExceptionPage();

app.UseDefaultFiles();
Expand Down
Loading

0 comments on commit 68ef1f8

Please sign in to comment.