feat(LogExtensions): Erstellen und Hinzufügen der Methode „LogCurlAsync“ zur Log-Curl-Methode der Anfrage

This commit is contained in:
tekh 2025-08-18 12:42:16 +02:00
parent a7f02e1079
commit b3a27ba24f

View File

@ -0,0 +1,41 @@
using Microsoft.Extensions.Logging;
using System.Text;
namespace Leanetec.EConnect.Infrastructure;
public static class LogExtensions
{
public static async Task LogCurlAsync<TCategoryName>(this ILogger<TCategoryName> logger, HttpClient client, HttpMethod method, string url, HttpContent? content, LogLevel logLevel = LogLevel.Information)
{
var sb = new StringBuilder();
sb.Append("curl");
// Method
sb.Append($" -X {method.Method}");
// Headers
foreach (var header in client.DefaultRequestHeaders)
{
sb.Append($" -H \"{header.Key}: {string.Join(", ", header.Value)}\"");
}
if (content != null)
{
foreach (var header in content.Headers)
{
sb.Append($" -H \"{header.Key}: {string.Join(", ", header.Value)}\"");
}
var body = await content.ReadAsStringAsync();
if (!string.IsNullOrWhiteSpace(body))
{
// Escape double quotes
body = body.Replace("\"", "\\\"");
sb.Append($" -d \"{body}\"");
}
}
sb.Append($" \"{url}\"");
logger.Log(logLevel, "{message}", sb.ToString());
}
}