Skip to content
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

Remove request logging from API #1469

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Threading.Tasks;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

worth looking at the git config to avoid seeing whitespace changes in the diff?

using GetIntoTeachingApi.Utils;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -28,11 +28,10 @@

public async Task Invoke(HttpContext context)
{
await LogRequest(context);
await LogResponse(context);
}

private static string ReadStream(Stream stream)

Check warning on line 34 in GetIntoTeachingApi/Middleware/RequestResponseLoggingMiddleware.cs

View workflow job for this annotation

GitHub Actions / sonarcloud

Remove the unused private method 'ReadStream'. (https://rules.sonarsource.com/csharp/RSPEC-1144)
{
const int bufferSize = 4096;

Expand Down Expand Up @@ -83,18 +82,6 @@
return _config.CompactLoggingPatterns.All(regex => !regex.IsMatch(input));
}

private async Task LogRequest(HttpContext context)
{
context.Request.EnableBuffering();

// Copy request body stream, resetting position for next middleware.
await using var stream = _recyclableMemoryStreamManager.GetStream();
await context.Request.Body.CopyToAsync(stream);
context.Request.Body.Position = 0;

LogInformation("HTTP Request", ReadStream(stream), context.Request);
}

private async Task LogResponse(HttpContext context)
{
// Keep track of the original response body stream (its read-once).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ public async Task Invoke_WithJsonPayloadAndCompactLoggingPath_LogsRequestAndResp
_context.Request.QueryString,
Payload = string.Empty,
};

_mockLogger.VerifyInformationWasCalled($"HTTP Request: {expectedInfo}");

_mockLogger.VerifyInformationWasCalled($"HTTP Response: {expectedInfo}");
}

Expand Down Expand Up @@ -88,8 +87,7 @@ public async Task Invoke_WithJsonPayload_LogsRequestAndResponseWithRedactedPaylo
_context.Request.QueryString,
Payload = redactedJson,
};

_mockLogger.VerifyInformationWasCalled($"HTTP Request: {expectedInfo}");

_mockLogger.VerifyInformationWasCalled($"HTTP Response: {expectedInfo}");
}

Expand All @@ -108,8 +106,7 @@ public async Task Invoke_WithNonJsonPayload_LogsRequestAndResponseWithEmptyPaylo
_context.Request.QueryString,
Payload = string.Empty,
};

_mockLogger.VerifyInformationWasCalled($"HTTP Request: {expectedInfo}");

_mockLogger.VerifyInformationWasCalled($"HTTP Response: {expectedInfo}");
}

Expand Down
Loading