You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Amazon.Lambda.AspNetCoreServer.Hosting: Response size logging (middleware) doesn't work in Lambda, but works from CLI #2040
And for our asp.net code that runs in fargate, this has worked fine to capture the response size, and ensure it gets saved for serilog:
public class MySizeLoggingMiddleware
{
private readonly RequestDelegate _next;
private readonly Serilog.IDiagnosticContext diagnosticContext;
public MySizeLoggingMiddleware(RequestDelegate next, Serilog.IDiagnosticContext diagnosticContext)
{
_next = next;
this.diagnosticContext = diagnosticContext;
}
public async Task InvokeAsync(HttpContext context)
{
if (context.Response.HasStarted) {
throw new Exception("Cannot buffer response, it has already started");
}
var originalStream = context.Response.Body;
using var bufferedStream = new MemoryStream();
context.Response.Body = bufferedStream;
try {
await _next(context);
} catch (Exception) {
Log.Warning("Exception was thrown, resetting body (developer exception page might write to it)");
context.Response.Body = originalStream;
throw;
}
// Get the buffered content size, and save it for serilog request logging
long contentSize = bufferedStream.Length;
diagnosticContext.Set("ResponseSize", contentSize);
context.Response.Body = originalStream;
bufferedStream.Seek(0, SeekOrigin.Begin);
await bufferedStream.CopyToAsync(originalStream);
}
}
However when using Amazon.Lambda.AspNetCoreServer.Hosting, inside Lambda, the size is always reported & logged as 0, meaning nothing is actually writing to the memory stream I'm creating. The same codebase works fine when run using dotnet run.
Regression Issue
Select this option if this issue appears to be a regression.
Fixed in Amazon.Lambda.AspNetCoreServer 10.1.1 (release_2026-05-18). Root cause was the same as #1702: InvokeFeatures.Set<TFeature> did not bump the feature collection revision, so ASP.NET Core kept writing to the original body stream after middleware replaced Response.Body.
Please reopen if it still reproduces on 10.1.1 or later.
Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.
Activity
kjpgit commentedon Apr 10, 2025
Microsoft still has no better way to get metrics on server->client bytes sent
dotnet/aspnetcore#38919
So we still need some hacky middleware like what I posted above, to continue to work.
dscpinheiro commentedon Oct 20, 2026
Fixed in
Amazon.Lambda.AspNetCoreServer10.1.1 (release_2026-05-18). Root cause was the same as #1702:InvokeFeatures.Set<TFeature>did not bump the feature collection revision, so ASP.NET Core kept writing to the original body stream after middleware replacedResponse.Body.Please reopen if it still reproduces on 10.1.1 or later.
github-actions commentedon Oct 20, 2026
Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.