using System.Diagnostics;
using System.IO;
using System.Security.Claims;
using System.Text;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using WmsLite.Api.Models;
using WmsLite.Api.Repositories.Interfaces;
namespace WmsLite.Api.Infrastructure.HttpLogging;
///
/// 记录 HTTP 方法、状态码、请求/响应正文(约定:GET/HEAD/OPTIONS 仅记请求;POST/PUT/PATCH/DELETE 记请求+响应;
/// 路径命中 PathsOmittingResponseBody 时 POST 等也不记响应体)、与 ILogger 格式化日志;可选写入 http_access_log(不含 SQL)。
///
public sealed class HttpRequestResponseLoggingMiddleware
{
private readonly RequestDelegate _next;
private readonly HttpLoggingOptions _options;
private readonly ILogger _logger;
private readonly IServiceScopeFactory _scopeFactory;
public HttpRequestResponseLoggingMiddleware(
RequestDelegate next,
IOptions options,
ILogger logger,
IServiceScopeFactory scopeFactory)
{
_next = next;
_options = options.Value;
_logger = logger;
_scopeFactory = scopeFactory;
}
public async Task InvokeAsync(HttpContext context)
{
if (!_options.Enabled)
{
await _next(context);
return;
}
HttpSqlLogContext.StartRequest(context);
var request = context.Request;
var pathValue = request.Path.Value ?? string.Empty;
var omitRequestBody = ShouldOmitRequestBody(pathValue);
var requestContentRaw = await ReadRequestContentAsync(request);
var requestContent = omitRequestBody
? "(omitted: sensitive path — request body not logged)"
: SanitizeForLog(requestContentRaw);
var sw = Stopwatch.StartNew();
var originalBody = context.Response.Body;
await using var buffer = new MemoryStream();
context.Response.Body = buffer;
try
{
await _next(context);
}
finally
{
sw.Stop();
buffer.Position = 0;
var captureResponse = ShouldCaptureResponseBody(request.Method, pathValue, out var responseOmittedReason);
string responseText;
if (captureResponse)
{
responseText = await ReadStreamAsStringAsync(buffer);
}
else
{
responseText = string.Empty;
}
buffer.Position = 0;
await buffer.CopyToAsync(originalBody);
context.Response.Body = originalBody;
var responseForLog = captureResponse
? SanitizeForLog(responseText)
: responseOmittedReason;
var sql = HttpSqlLogContext.GetSqlSnapshot(context);
var formatted = HttpAccessLogFormatter.Build(
request.Method,
request.Path.Value,
context.Response.StatusCode,
requestContent,
responseForLog,
sql);
_logger.LogInformation("{AccessLog}", formatted);
if (_options.PersistToDatabase)
{
await TryPersistAccessLogAsync(
context,
request,
requestContent,
captureResponse ? responseText : null,
sw.ElapsedMilliseconds,
omitRequestBody);
}
}
}
///
/// 约定:GET/HEAD/OPTIONS 只记请求;POST/PUT/PATCH/DELETE 记请求+响应。
/// 例外:路径命中 时视为查询型接口,不记响应体。
///
private bool ShouldCaptureResponseBody(string method, string pathValue, out string omittedReason)
{
omittedReason = string.Empty;
var m = method.ToUpperInvariant();
if (HttpMethods.IsGet(m) || HttpMethods.IsHead(m) || HttpMethods.IsOptions(m))
{
omittedReason = "(omitted: GET/HEAD/OPTIONS — request only)";
return false;
}
if (m is not ("POST" or "PUT" or "PATCH" or "DELETE"))
{
omittedReason = "(omitted: only POST/PUT/PATCH/DELETE capture response body)";
return false;
}
var keywords = _options.PathsOmittingResponseBody;
if (keywords is { Length: > 0 })
{
foreach (var k in keywords)
{
if (string.IsNullOrWhiteSpace(k))
{
continue;
}
if (pathValue.Contains(k.Trim(), StringComparison.OrdinalIgnoreCase))
{
omittedReason = "(omitted: path matches PathsOmittingResponseBody whitelist)";
return false;
}
}
}
return true;
}
private async Task TryPersistAccessLogAsync(
HttpContext context,
HttpRequest request,
string requestContentForDisplay,
string? responseText,
long elapsedMs,
bool omitRequestBody)
{
try
{
var queryForDb = request.QueryString.HasValue ? request.QueryString.Value : null;
string? bodyForDb;
if (HttpMethods.IsGet(request.Method) || HttpMethods.IsHead(request.Method) || HttpMethods.IsOptions(request.Method))
{
bodyForDb = null;
}
else
{
bodyForDb = omitRequestBody || string.IsNullOrEmpty(requestContentForDisplay)
? null
: requestContentForDisplay;
}
var entity = new HttpAccessLog
{
TraceId = context.TraceIdentifier,
HttpMethod = request.Method,
Path = request.Path.Value ?? string.Empty,
QueryString = string.IsNullOrEmpty(queryForDb) ? null : queryForDb,
StatusCode = context.Response.StatusCode,
RequestBody = bodyForDb,
ResponseBody = responseText is null || string.IsNullOrEmpty(responseText)
? null
: SanitizeForLog(responseText),
DurationMs = elapsedMs > int.MaxValue ? int.MaxValue : (int)elapsedMs,
ClientIp = GetClientIp(context),
UserId = context.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value
?? context.User?.FindFirst("sub")?.Value
};
using var scope = _scopeFactory.CreateScope();
var repo = scope.ServiceProvider.GetRequiredService();
await repo.InsertAsync(entity);
}
catch (Exception ex)
{
_logger.LogWarning(ex, "写入 http_access_log 失败。");
}
}
private bool ShouldOmitRequestBody(string pathValue)
{
var keywords = _options.PathsOmittingRequestBody;
if (keywords is not { Length: > 0 })
{
return false;
}
foreach (var k in keywords)
{
if (!string.IsNullOrWhiteSpace(k) && pathValue.Contains(k.Trim(), StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
return false;
}
private string SanitizeForLog(string? content)
{
if (string.IsNullOrEmpty(content))
{
return string.Empty;
}
var lower = content.ToLowerInvariant();
foreach (var field in _options.SensitiveFieldNames ?? [])
{
if (string.IsNullOrWhiteSpace(field))
{
continue;
}
if (lower.Contains($"\"{field.Trim().ToLowerInvariant()}\"", StringComparison.Ordinal))
{
return "(redacted: sensitive fields)";
}
}
return content;
}
private static string? GetClientIp(HttpContext context)
{
var forwarded = context.Request.Headers["X-Forwarded-For"].ToString();
if (!string.IsNullOrWhiteSpace(forwarded))
{
var first = forwarded.Split(',')[0].Trim();
if (!string.IsNullOrEmpty(first))
{
return first;
}
}
return context.Connection.RemoteIpAddress?.ToString();
}
private static async Task ReadRequestContentAsync(HttpRequest request)
{
if (HttpMethods.IsGet(request.Method) || HttpMethods.IsHead(request.Method) || HttpMethods.IsOptions(request.Method))
{
return request.QueryString.HasValue ? request.QueryString.Value! : string.Empty;
}
request.EnableBuffering();
request.Body.Position = 0;
using var reader = new StreamReader(
request.Body,
Encoding.UTF8,
detectEncodingFromByteOrderMarks: false,
bufferSize: 1024,
leaveOpen: true);
var body = await reader.ReadToEndAsync();
request.Body.Position = 0;
return body;
}
private static async Task ReadStreamAsStringAsync(Stream stream)
{
if (stream.Length == 0)
{
return string.Empty;
}
stream.Position = 0;
using var reader = new StreamReader(stream, Encoding.UTF8, detectEncodingFromByteOrderMarks: false, leaveOpen: true);
return await reader.ReadToEndAsync();
}
}