Add Serilog console logging across the API
Information at endpoint and service-method boundaries, Debug in deeper helpers, Warning before expected/recoverable failures (not-found, validation, agent tool errors), Error on caught exceptions. Serilog wraps the exception handler so request-completion logs report the resolved status code rather than the raw exception. Never logs prose bodies or the Anthropic API key.
This commit is contained in:
@@ -11,9 +11,17 @@ using Novelly.Api.Projects;
|
||||
using Novelly.Api.Questions;
|
||||
using Novelly.Api.Scenes;
|
||||
using Novelly.Api.Tags;
|
||||
using Serilog;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// AddSerilog (not UseSerilog) so it becomes an additional logging provider rather than
|
||||
// replacing the one AddServiceDefaults wires up for the Aspire dashboard.
|
||||
builder.Services.AddSerilog((services, config) => config
|
||||
.ReadFrom.Configuration(builder.Configuration)
|
||||
.ReadFrom.Services(services)
|
||||
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {SourceContext}: {Message:lj}{NewLine}{Exception}"));
|
||||
|
||||
builder.AddServiceDefaults();
|
||||
builder.Services.AddNovelly(builder.Configuration);
|
||||
builder.Services.AddOpenApi();
|
||||
@@ -41,6 +49,11 @@ using (var scope = app.Services.CreateScope())
|
||||
await scope.ServiceProvider.GetRequiredService<NovelDbContext>().Database.MigrateAsync();
|
||||
}
|
||||
|
||||
// Serilog's request logging wraps the exception handler (registered first = outermost)
|
||||
// so it reads the status code the handler already resolved, rather than seeing the raw
|
||||
// exception fly past and misreporting a handled 404 as a 500.
|
||||
app.UseSerilogRequestLogging();
|
||||
|
||||
app.UseExceptionHandler(handler => handler.Run(async context =>
|
||||
{
|
||||
var exception = context.Features.Get<IExceptionHandlerFeature>()?.Error;
|
||||
@@ -57,6 +70,10 @@ app.UseExceptionHandler(handler => handler.Run(async context =>
|
||||
{
|
||||
app.Logger.LogError(exception, "Unhandled exception on {Path}", context.Request.Path);
|
||||
}
|
||||
else
|
||||
{
|
||||
app.Logger.LogWarning(exception, "Handled {StatusCode} on {Path}: {Title}", status, context.Request.Path, title);
|
||||
}
|
||||
|
||||
await Results
|
||||
.Problem(title: title, detail: exception?.Message, statusCode: status)
|
||||
|
||||
Reference in New Issue
Block a user