diff --git a/src/Leanetec.EConnect.Infrastructure/LogExtensions.cs b/src/Leanetec.EConnect.Infrastructure/LogExtensions.cs index 9baa4bf..b791ba0 100644 --- a/src/Leanetec.EConnect.Infrastructure/LogExtensions.cs +++ b/src/Leanetec.EConnect.Infrastructure/LogExtensions.cs @@ -15,10 +15,10 @@ public static class LogExtensions int maxLength = 1000, LogLevel logLevel = LogLevel.Information) { - var curl = string.Concat(client.GenerateCurlInString( + var curl = client.GenerateCurlInString( request, config - ).AsSpan(0, maxLength), "...[truncated]"); + ).Truncate(maxLength); logger?.Log(logLevel, "{curl}", curl); } @@ -26,20 +26,20 @@ public static class LogExtensions this ILogger logger, HttpClient client, HttpMethod method, - string uri = "", + string uri = "/", HttpRequestHeaders? headers = null, HttpContent? content = null, Action? config = null, int maxLength = 1000, LogLevel logLevel = LogLevel.Information) { - var curl = string.Concat(client.GenerateCurlInString( + var curl = client.GenerateCurlInString( method, uri, headers, content, config - ).AsSpan(0, maxLength), "...[truncated]"); + ).Truncate(maxLength); logger?.Log(logLevel, "{curl}", curl); } @@ -54,13 +54,26 @@ public static class LogExtensions int maxLength = 1000, LogLevel logLevel = LogLevel.Information) { - var curl = string.Concat(client.GenerateCurlInString( + var curl = client.GenerateCurlInString( method, uri, headers, content, config - ).AsSpan(0, maxLength), "...[truncated]"); + ).Truncate(maxLength); logger?.Log(logLevel, "{curl}", curl); } + + /// + /// Truncates the string to the specified max length and appends a suffix if truncated. + /// + 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; + } } \ No newline at end of file