Add logging when api request comes in bad

This commit is contained in:
2022-07-17 22:08:14 -04:00
parent 2e9a3108f4
commit a5737a510d
4 changed files with 12 additions and 4 deletions

View File

@@ -1,6 +1,7 @@
using System.Net.Mime;
using System.Text;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Treestar.Shared.Models;
@@ -10,10 +11,12 @@ public abstract class ApiAccessLayer
{
private readonly HttpClient _httpClient;
private readonly IAccessLayerAuthenticationProvider _authenticationProvider;
protected readonly ILogger Logger;
protected ApiAccessLayer(string apiBaseUrl, IAccessLayerAuthenticationProvider authenticationProvider)
protected ApiAccessLayer(string apiBaseUrl, IAccessLayerAuthenticationProvider authenticationProvider, ILogger logger)
{
_authenticationProvider = authenticationProvider;
Logger = logger;
var handler = new HttpClientHandler()
{
ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
@@ -26,6 +29,10 @@ public abstract class ApiAccessLayer
{
await _authenticationProvider.AddAuthentication(message);
var response = await _httpClient.SendAsync(message);
if (!response.IsSuccessStatusCode)
{
Logger.LogError("Response returned status code {statusCode} with reason {reason} and content {content}", response.StatusCode, response.ReasonPhrase, await response.Content.ReadAsStringAsync());
}
return new HttpResponseWrapper()
{
HttpResponseMessage = response

View File

@@ -8,7 +8,7 @@ namespace WebNovelPortal.AccessLayers;
public class WebApiAccessLayer : ApiAccessLayer
{
public WebApiAccessLayer(string apiBaseUrl, IAccessLayerAuthenticationProvider authenticationProvider) : base(apiBaseUrl, authenticationProvider)
public WebApiAccessLayer(string apiBaseUrl, IAccessLayerAuthenticationProvider authenticationProvider, ILogger<WebApiAccessLayer> logger) : base(apiBaseUrl, authenticationProvider, logger)
{
}

View File

@@ -11,7 +11,7 @@ var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddScoped<IAccessLayerAuthenticationProvider, BlazorAccessLayerAuthProvider>();
builder.Services.AddScoped(fac => new WebApiAccessLayer(builder.Configuration["WebAPIUrl"], fac.GetRequiredService<IAccessLayerAuthenticationProvider>()));
builder.Services.AddScoped(fac => new WebApiAccessLayer(builder.Configuration["WebAPIUrl"], fac.GetRequiredService<IAccessLayerAuthenticationProvider>(), fac.GetRequiredService<ILogger<WebApiAccessLayer>>()));
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddHttpContextAccessor();

View File

@@ -5,5 +5,6 @@
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
},
"WebAPIUrl": "http://seedboxnew:5433/api/"
}