Amazon.Lambda.AspNetCoreServer.Hosting: Response size logging (middleware) doesn't work in Lambda, but works from CLI
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 30/100
Research direction
Reproduce the response-size logging middleware with Amazon.Lambda.AspNetCoreServer.Hosting on .NET 8 in Lambda, then compare its response-body handling with the same code under dotnet run. Done means the middleware captures a nonzero response size and Serilog logs it in Lambda as well.
Written by the indexing model from the issue text.
Description
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
- Dominant language
- C#
- Stars
- 1.7k
- Forks
- 503
- Avg merge
- 1d 18h
- Merged PRs (30d)
- 21
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from aws/aws-lambda-dotnet
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
aws/aws-lambda-dotnet#2571 · 1 comment ·
-
bug module/aspnetcore-support p2 queued
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
aws/aws-lambda-dotnet#1123 · 4 comments · 1 reaction ·
-
feature-request
Difficulty 4/5 3-5 days Newbie friendliness 68/100
aws/aws-lambda-dotnet#2572 · 1 comment ·
-
feature-request needs-triage
Difficulty 4/5 3-5 days Newbie friendliness 48/100
aws/aws-lambda-dotnet#2551 · 1 comment ·
-
feature-request needs-review
Difficulty 5/5 Over a week Newbie friendliness 35/100
aws/aws-lambda-dotnet#2519 ·
All issues in aws/aws-lambda-dotnet
Similar issues
-
type/automation type/tech-debt
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
t/bug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
ci-failure-cause test-failure
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
area:auth FE mvp P3
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
klasolsson81/jobbliggaren#1788 ·