fix(server): the current log downloads while the server keeps writing it

GET /api/v1/logs/files/{name} served the file with Results.File, which sets
Content-Length from the file's size and then copies it to its end. The current
log grows whenever the server logs, so a line written meanwhile made Kestrel
abort the response ("too many bytes written"); AdminTests failed on it once in
three full runs. The endpoint now sends the bytes the file held when it was
listed through FileResponses.Serve, a fixed-length, range-capable slice.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-06 21:19:30 +00:00
1 parent 9c00977dab
commit 9f03c07fdf
2 files changed
+36 -3

No files matched your search

+4 -3
View File
@@ -29,10 +29,11 @@ public static class AdminEndpoints
ProtocolJson.Default.ListLogFileDto));
logs.MapGet("/files/{name}", (HttpContext context, string name, ServerOptions options) =>
{
// Only names of existing log files are served: the name never becomes part of a path.
// Only names of existing log files are served: the name never becomes part of a path. The current log keeps
// growing while it is sent, so the response holds the bytes it had when listed, as its Content-Length says.
if (LogFiles(options).FirstOrDefault(f => f.Name == name) is not { } file) return ApiErrors.NotFound(context, "log");
context.Response.Headers.CacheControl = "no-store";
return Results.File(file.FullName, "text/plain; charset=utf-8", file.Name, file.LastWriteTimeUtc, enableRangeProcessing: true);
return FileResponses.Serve(context, file, 0, file.Length, "text/plain; charset=utf-8", "no-store", downloadName: file.Name)
?? ApiErrors.NotFound(context, "log");
});
}