Skip to content

Amazon.Lambda.AspNetCoreServer.Hosting: Response size logging (middleware) doesn't work in Lambda, but works from CLI #2040

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 task
kjpgit opened this issue Apr 10, 2025 · 1 comment
Labels
bug This issue is a bug. p2 This is a standard priority issue queued

Comments

@kjpgit
Copy link

kjpgit commented Apr 10, 2025

Describe the bug

Not sure if this a bug or feature request, but how can I log all response sizes?

We currently use serilog request logging middleware: https://github.com/serilog/serilog-aspnetcore?tab=readme-ov-file#request-logging

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.

Expected Behavior

.

Current Behavior

.

Reproduction Steps

.

Possible Solution

No response

Additional Information/Context

No response

AWS .NET SDK and/or Package version used

Amazon.Lambda.AspNetCoreServer.Hosting

Targeted .NET Platform

.net 8

Operating System and version

lambda

@kjpgit kjpgit added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Apr 10, 2025
@kjpgit
Copy link
Author

kjpgit commented 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.

@philasmar philasmar added feature-request A feature should be added or improved. and removed bug This issue is a bug. labels Apr 23, 2025
@eddiemcs3 eddiemcs3 added bug This issue is a bug. p2 This is a standard priority issue queued and removed feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels May 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. p2 This is a standard priority issue queued
Projects
None yet
Development

No branches or pull requests

3 participants