app: wire Serilog into SVSim.EmulatedEntrypoint

Registers Serilog via SVSim.Hosting.UseSvSimSerilog("svsim-api") right
after CreateBuilder and wraps Main in try/catch/finally so a fatal
startup exception logs before CloseAndFlush.

Also updates the SVSim.Hosting extension to gate the file sink on entry
assembly rather than the ASP.NET environment name. WebApplicationFactory
sets the environment via a hook that resolves after both CreateBuilder
and the UseSerilog callback see their environment view, so
IsEnvironment("Testing") is unreliable in tests. The entry assembly
under `dotnet test` is always "testhost", which is a reliable signal.

appsettings.json grows a Serilog section mirroring the existing
Logging:LogLevel overrides; the Logging block itself is left in place
per the spec's follow-up cleanup note.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
gamer147
2026-07-04 02:07:41 -04:00
parent a7843adfe6
commit 9a47ac8d90
4 changed files with 53 additions and 4 deletions

View File

@@ -20,6 +20,8 @@ using SVSim.EmulatedEntrypoint.Middlewares;
using SVSim.EmulatedEntrypoint.Security.SteamSessionAuthentication;
using SVSim.EmulatedEntrypoint.Services;
using SVSim.BattleNode.Hosting;
using Serilog;
using SVSim.Hosting;
namespace SVSim.EmulatedEntrypoint;
@@ -29,6 +31,13 @@ public class Program
{
var builder = WebApplication.CreateBuilder(args);
// Always register Serilog. The file sink self-gates on the Testing environment inside
// the UseSvSimSerilog callback — a caller-side check here would be too early because
// WebApplicationFactory applies UseEnvironment("Testing") after CreateBuilder returns.
builder.Host.UseSvSimSerilog("svsim-api");
try
{
// Add services to the container.
builder.Services.AddControllers().AddJsonOptions(opt =>
@@ -253,5 +262,15 @@ public class Program
app.MapControllers();
app.Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly");
throw;
}
finally
{
Log.CloseAndFlush();
}
}
}

View File

@@ -39,6 +39,7 @@
<ItemGroup>
<ProjectReference Include="..\SVSim.Database\SVSim.Database.csproj" />
<ProjectReference Include="..\SVSim.BattleNode\SVSim.BattleNode.csproj" />
<ProjectReference Include="..\SVSim.Hosting\SVSim.Hosting.csproj" />
</ItemGroup>
<ItemGroup>

View File

@@ -9,6 +9,18 @@
"Microsoft.EntityFrameworkCore.Infrastructure": "Warning"
}
},
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft.AspNetCore": "Warning",
"Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware": "Information",
"Microsoft.EntityFrameworkCore": "Warning",
"Microsoft.EntityFrameworkCore.Database.Command": "Warning",
"Microsoft.EntityFrameworkCore.Infrastructure": "Warning"
}
}
},
"ConnectionStrings": {
"ApplicationDb": "Host=localhost;Database=svsim;Username=postgres;password=postgres"
},