feat(LogExtensions): Erstellen und Hinzufügen der Methode „LogCurlAsync“ zur Log-Curl-Methode der Anfrage
This commit is contained in:
parent
a7f02e1079
commit
b3a27ba24f
41
src/Leanetec.EConnect.Infrastructure/LogExtensions.cs
Normal file
41
src/Leanetec.EConnect.Infrastructure/LogExtensions.cs
Normal 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user