chore: implements Debug Logging for request and response including body
This commit is contained in:
@@ -17,7 +17,8 @@ namespace Novaloop.PaymoApi.Extensions
|
|||||||
{
|
{
|
||||||
services.Configure(options);
|
services.Configure(options);
|
||||||
var resolvedOptions = (IOptions<ApiOptions>) services.BuildServiceProvider().GetService(typeof(IOptions<ApiOptions>));
|
var resolvedOptions = (IOptions<ApiOptions>) services.BuildServiceProvider().GetService(typeof(IOptions<ApiOptions>));
|
||||||
services.AddHttpClient<ApiClient>(client => { client.BaseAddress = new Uri(resolvedOptions.Value.BaseUrl); });
|
services.AddHttpClient<ApiClient>(client => { client.BaseAddress = new Uri(resolvedOptions.Value.BaseUrl); })
|
||||||
|
.AddHttpMessageHandler(s => s.GetService<LoggingHandler>());
|
||||||
|
|
||||||
// ClientContacts
|
// ClientContacts
|
||||||
services.AddTransient<IBaseApi<ClientContactsResponse, ClientContact>, BaseApi<ClientContactsResponse, ClientContact>>();
|
services.AddTransient<IBaseApi<ClientContactsResponse, ClientContact>, BaseApi<ClientContactsResponse, ClientContact>>();
|
||||||
@@ -31,6 +32,9 @@ namespace Novaloop.PaymoApi.Extensions
|
|||||||
services.AddTransient<IBaseApi<ClientsResponse, Client>, BaseApi<ClientsResponse, Client>>();
|
services.AddTransient<IBaseApi<ClientsResponse, Client>, BaseApi<ClientsResponse, Client>>();
|
||||||
services.AddTransient<IClientContactsApi, ClientContactsApi>();
|
services.AddTransient<IClientContactsApi, ClientContactsApi>();
|
||||||
|
|
||||||
|
// Shared
|
||||||
|
services.AddTransient<LoggingHandler>();
|
||||||
|
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
45
src/Shared/LoggingHandler.cs
Normal file
45
src/Shared/LoggingHandler.cs
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
using System.Net.Http;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace Novaloop.PaymoApi.Shared
|
||||||
|
{
|
||||||
|
public class LoggingHandler : DelegatingHandler
|
||||||
|
{
|
||||||
|
private readonly ILogger<LoggingHandler> _logger;
|
||||||
|
|
||||||
|
public LoggingHandler(ILogger<LoggingHandler> logger)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LoggingHandler(HttpMessageHandler innerHandler, ILogger<LoggingHandler> logger)
|
||||||
|
: base(innerHandler)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
_logger.LogDebug($"Request:\n{request}");
|
||||||
|
if (request.Content != null)
|
||||||
|
{
|
||||||
|
var body = await request.Content.ReadAsStringAsync();
|
||||||
|
_logger.LogDebug(body);
|
||||||
|
}
|
||||||
|
|
||||||
|
var response = await base.SendAsync(request, cancellationToken);
|
||||||
|
|
||||||
|
_logger.LogDebug($"Response:\n{response}");
|
||||||
|
if (response.Content != null)
|
||||||
|
{
|
||||||
|
var body = await response.Content.ReadAsStringAsync();
|
||||||
|
_logger.LogDebug(body);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.Logging.Abstractions;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using Novaloop.PaymoApi.Extensions;
|
using Novaloop.PaymoApi.Extensions;
|
||||||
using Novaloop.PaymoApi.Shared;
|
using Novaloop.PaymoApi.Shared;
|
||||||
@@ -11,7 +12,7 @@ namespace Novaloop.PaymoApi.Tests
|
|||||||
{
|
{
|
||||||
public static ApiClient GeneratePaymoApiClient()
|
public static ApiClient GeneratePaymoApiClient()
|
||||||
{
|
{
|
||||||
return new ApiClient(new HttpClient {BaseAddress = new Uri("https://app.paymoapp.com/")});
|
return new ApiClient(new HttpClient(new LoggingHandler(new HttpClientHandler(), new NullLogger<LoggingHandler>())) {BaseAddress = new Uri("https://app.paymoapp.com/")});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IOptions<ApiOptions> GenerateOptions()
|
public static IOptions<ApiOptions> GenerateOptions()
|
||||||
|
|||||||
Reference in New Issue
Block a user