using System.Diagnostics;
using System.Net.Http.Json;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Unicode;
using Microsoft.Extensions.Logging;
namespace MS.Microservice.Core.Net.Http;
/// 保持 HTTP 编码、请求头隔离及原始取消/失败语义的通用 JSON 请求辅助。
/// 业务重试由调用方决定;默认诊断不记录 URL 参数或正文,避免把凭据和业务数据带进日志。
public class LogHttpClient(ILogger logger, HttpClient httpClient)
{
public JsonSerializerOptions JsonSerializerOptions = new()
{
PropertyNameCaseInsensitive = true,
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
};
public void Configure(string baseAddress, TimeSpan timeout)
{
httpClient.BaseAddress = new Uri(baseAddress);
httpClient.Timeout = timeout;
}
public ValueTask GetAsync(string requestUrl, object? body, CancellationToken cancellationToken = default)
=> SendAsync(HttpMethod.Get, QueryStringParameters.BuildUrl(requestUrl, body), null, null, cancellationToken);
public ValueTask GetAsync(string url, object? body, Dictionary headers,
CancellationToken cancellationToken = default)
=> SendAsync(HttpMethod.Get, QueryStringParameters.BuildUrl(url, body), null, headers, cancellationToken);
public ValueTask PostAsync(string url, object? body, CancellationToken cancellationToken = default)
=> SendAsync(HttpMethod.Post, url, body, null, cancellationToken);
public Task PostAsync(string url, object? body, Dictionary headers,
CancellationToken cancellationToken = default)
=> SendAsync(HttpMethod.Post, url, body, headers, cancellationToken).AsTask();
private async ValueTask SendAsync(HttpMethod method, string url, object? body,
Dictionary? headers, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
using var request = new HttpRequestMessage(method, url);
if (method == HttpMethod.Post)
request.Content = new StringContent(JsonSerializer.Serialize(body, JsonSerializerOptions), Encoding.UTF8, "application/json");
if (headers is not null)
foreach (var (name, value) in headers) request.Headers.Add(name, value);
var id = Guid.NewGuid();
var started = Stopwatch.GetTimestamp();
logger.LogInformation("HTTP {RequestId} {Method} started", id, method.Method);
try
{
using var response = await httpClient.SendAsync(request, cancellationToken);
response.EnsureSuccessStatusCode();
var result = await response.Content.ReadFromJsonAsync(JsonSerializerOptions, cancellationToken);
logger.LogInformation("HTTP {RequestId} {Method} completed {StatusCode} in {ElapsedMs} ms",
id, method.Method, (int)response.StatusCode, Stopwatch.GetElapsedTime(started).TotalMilliseconds);
return result;
}
catch (Exception exception)
{
// URLs, bodies and exception messages may contain credentials or personal data.
logger.LogInformation("HTTP {RequestId} {Method} ended {FailureType} in {ElapsedMs} ms",
id, method.Method, exception.GetType().Name, Stopwatch.GetElapsedTime(started).TotalMilliseconds);
throw;
}
}
}