feat(LogExtensions): add full url and use placeholder for binary-data
This commit is contained in:
parent
b3a27ba24f
commit
48a69f884e
@ -1,5 +1,6 @@
|
||||
using Leanetec.EConnect.Client.Interface;
|
||||
using Leanetec.EConnect.Domain.Entities;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System.Net.Http.Json;
|
||||
|
||||
@ -13,10 +14,13 @@ public class EConnectClient<TError> : IEConnectClient<TError> where TError : cl
|
||||
|
||||
private HttpClient Http => LazyHttp.Value;
|
||||
|
||||
public EConnectClient(IOptions<ClientOptions> options, IHttpClientFactory httpFactory)
|
||||
private readonly ILogger<EConnectClient<TError>>? _logger;
|
||||
|
||||
public EConnectClient(IOptions<ClientOptions> options, IHttpClientFactory httpFactory, ILogger<EConnectClient<TError>>? logger = null)
|
||||
{
|
||||
_options = options.Value;
|
||||
LazyHttp = new Lazy<HttpClient>(httpFactory.CreateEConnectClient);
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
private string? AddQueryString(string? route = null, object? queryParams = null)
|
||||
@ -127,6 +131,8 @@ public class EConnectClient<TError> : IEConnectClient<TError> where TError : cl
|
||||
|
||||
var res = await Http.PostAsync(route, content, cancel);
|
||||
|
||||
_logger?.LogCurlAsync(Http, HttpMethod.Post, route, content);
|
||||
|
||||
if (res.IsSuccessStatusCode)
|
||||
{
|
||||
return new()
|
||||
|
||||
@ -5,7 +5,7 @@ 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)
|
||||
public static async Task LogCurlAsync<TCategoryName>(this ILogger<TCategoryName> logger, HttpClient client, HttpMethod method, string? route = null, HttpContent? content = null, LogLevel logLevel = LogLevel.Information)
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
sb.Append("curl");
|
||||
@ -13,29 +13,47 @@ public static class LogExtensions
|
||||
// Method
|
||||
sb.Append($" -X {method.Method}");
|
||||
|
||||
// URL
|
||||
var fullUrl = string.Empty;
|
||||
if (client.BaseAddress is not null && route is not null)
|
||||
fullUrl = new Uri(client.BaseAddress, route).ToString();
|
||||
else if (route is not null)
|
||||
fullUrl = route;
|
||||
else if (client.BaseAddress is not null)
|
||||
fullUrl = client.BaseAddress.ToString();
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(fullUrl))
|
||||
sb.AppendLine($" \"{fullUrl}\"");
|
||||
|
||||
// Headers
|
||||
foreach (var header in client.DefaultRequestHeaders)
|
||||
{
|
||||
sb.Append($" -H \"{header.Key}: {string.Join(", ", header.Value)}\"");
|
||||
sb.AppendLine($"\t-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)}\"");
|
||||
sb.AppendLine($"\t-H \"{header.Key}: {string.Join(", ", header.Value)}\"");
|
||||
}
|
||||
|
||||
var body = await content.ReadAsStringAsync();
|
||||
if (!string.IsNullOrWhiteSpace(body))
|
||||
var mediaType = content.Headers.ContentType?.MediaType;
|
||||
if (mediaType is not null && mediaType.StartsWith("text") || mediaType!.Contains("json"))
|
||||
{
|
||||
// Escape double quotes
|
||||
body = body.Replace("\"", "\\\"");
|
||||
sb.Append($" -d \"{body}\"");
|
||||
var body = await content.ReadAsStringAsync();
|
||||
if (!string.IsNullOrWhiteSpace(body))
|
||||
{
|
||||
// Escape double quotes
|
||||
body = body.Replace("\"", "\\\"");
|
||||
sb.AppendLine($"\t-d \"{body}\"");
|
||||
}
|
||||
}
|
||||
else
|
||||
// Binary content, only placeholder in log
|
||||
sb.AppendLine("\t-d \"<binary data>\"");
|
||||
}
|
||||
|
||||
sb.Append($" \"{url}\"");
|
||||
|
||||
logger.Log(logLevel, "{message}", sb.ToString());
|
||||
}
|
||||
}
|
||||
@ -20,13 +20,11 @@ public class OrderController : ControllerBase
|
||||
{
|
||||
var res = await _mediator.Send(request, cancel);
|
||||
if(res.Ok)
|
||||
{
|
||||
return res.Data is null || !res.Data.Any() ? NotFound() : Ok(res.Data);
|
||||
}
|
||||
return res.Data is null || !res.Data.Any()
|
||||
? NotFound()
|
||||
: Ok(res.Data);
|
||||
else
|
||||
{
|
||||
return StatusCode(res.StatusCodeInt, res?.Error);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost("document")]
|
||||
@ -40,13 +38,8 @@ public class OrderController : ControllerBase
|
||||
|
||||
var res = await _mediator.Send(request.ToUploadDocument(streamContent), cancel);
|
||||
|
||||
if (res.Ok)
|
||||
{
|
||||
return StatusCode(res.StatusCodeInt);
|
||||
}
|
||||
else
|
||||
{
|
||||
return StatusCode(res.StatusCodeInt, res?.Error);
|
||||
}
|
||||
return res.Ok
|
||||
? StatusCode(res.StatusCodeInt)
|
||||
: StatusCode(res.StatusCodeInt, res?.Error);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user