refactor: Curl-Logging erweitert und String-Truncation eingeführt

- Entfernt manuelles Abschneiden von Curl-Strings und ersetzt durch die neue `Truncate`-Extension.
- Standardwert für `uri` im zweiten LogCurl-Overload auf "/" gesetzt.
- Verbessert Lesbarkeit und Wiederverwendbarkeit der Logik für Curl-Strings.
This commit is contained in:
tekh 2025-08-18 17:11:14 +02:00
parent 66cfe0525c
commit 49c4960f05

View File

@ -15,10 +15,10 @@ public static class LogExtensions
int maxLength = 1000, int maxLength = 1000,
LogLevel logLevel = LogLevel.Information) LogLevel logLevel = LogLevel.Information)
{ {
var curl = string.Concat(client.GenerateCurlInString( var curl = client.GenerateCurlInString(
request, request,
config config
).AsSpan(0, maxLength), "...[truncated]"); ).Truncate(maxLength);
logger?.Log(logLevel, "{curl}", curl); logger?.Log(logLevel, "{curl}", curl);
} }
@ -26,20 +26,20 @@ public static class LogExtensions
this ILogger<TCategoryName> logger, this ILogger<TCategoryName> logger,
HttpClient client, HttpClient client,
HttpMethod method, HttpMethod method,
string uri = "", string uri = "/",
HttpRequestHeaders? headers = null, HttpRequestHeaders? headers = null,
HttpContent? content = null, HttpContent? content = null,
Action<StringConfig>? config = null, Action<StringConfig>? config = null,
int maxLength = 1000, int maxLength = 1000,
LogLevel logLevel = LogLevel.Information) LogLevel logLevel = LogLevel.Information)
{ {
var curl = string.Concat(client.GenerateCurlInString( var curl = client.GenerateCurlInString(
method, method,
uri, uri,
headers, headers,
content, content,
config config
).AsSpan(0, maxLength), "...[truncated]"); ).Truncate(maxLength);
logger?.Log(logLevel, "{curl}", curl); logger?.Log(logLevel, "{curl}", curl);
} }
@ -54,13 +54,26 @@ public static class LogExtensions
int maxLength = 1000, int maxLength = 1000,
LogLevel logLevel = LogLevel.Information) LogLevel logLevel = LogLevel.Information)
{ {
var curl = string.Concat(client.GenerateCurlInString( var curl = client.GenerateCurlInString(
method, method,
uri, uri,
headers, headers,
content, content,
config config
).AsSpan(0, maxLength), "...[truncated]"); ).Truncate(maxLength);
logger?.Log(logLevel, "{curl}", curl); logger?.Log(logLevel, "{curl}", curl);
} }
/// <summary>
/// Truncates the string to the specified max length and appends a suffix if truncated.
/// </summary>
public static string Truncate(this string? value, int maxLength, string suffix = "...[truncated]")
{
if (string.IsNullOrEmpty(value) || maxLength <= 0)
return string.Empty;
return value.Length <= maxLength
? value
: value[..maxLength] + suffix;
}
} }