From a5737a510d2b79d9ccdfb7e448252192c2b2561d Mon Sep 17 00:00:00 2001 From: littlefoot Date: Sun, 17 Jul 2022 22:08:14 -0400 Subject: [PATCH] Add logging when api request comes in bad --- Treestar.Shared/AccessLayers/ApiAccessLayer.cs | 9 ++++++++- WebNovelPortal/AccessLayers/WebApiAccessLayer.cs | 2 +- WebNovelPortal/Program.cs | 2 +- WebNovelPortal/appsettings.Development.json | 3 ++- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Treestar.Shared/AccessLayers/ApiAccessLayer.cs b/Treestar.Shared/AccessLayers/ApiAccessLayer.cs index 873529f..caeb9ff 100644 --- a/Treestar.Shared/AccessLayers/ApiAccessLayer.cs +++ b/Treestar.Shared/AccessLayers/ApiAccessLayer.cs @@ -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 diff --git a/WebNovelPortal/AccessLayers/WebApiAccessLayer.cs b/WebNovelPortal/AccessLayers/WebApiAccessLayer.cs index 36f7220..70af227 100644 --- a/WebNovelPortal/AccessLayers/WebApiAccessLayer.cs +++ b/WebNovelPortal/AccessLayers/WebApiAccessLayer.cs @@ -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 logger) : base(apiBaseUrl, authenticationProvider, logger) { } diff --git a/WebNovelPortal/Program.cs b/WebNovelPortal/Program.cs index 23cc23e..18042cd 100644 --- a/WebNovelPortal/Program.cs +++ b/WebNovelPortal/Program.cs @@ -11,7 +11,7 @@ var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddScoped(); -builder.Services.AddScoped(fac => new WebApiAccessLayer(builder.Configuration["WebAPIUrl"], fac.GetRequiredService())); +builder.Services.AddScoped(fac => new WebApiAccessLayer(builder.Configuration["WebAPIUrl"], fac.GetRequiredService(), fac.GetRequiredService>())); builder.Services.AddRazorPages(); builder.Services.AddServerSideBlazor(); builder.Services.AddHttpContextAccessor(); diff --git a/WebNovelPortal/appsettings.Development.json b/WebNovelPortal/appsettings.Development.json index 770d3e9..0dfa2d2 100644 --- a/WebNovelPortal/appsettings.Development.json +++ b/WebNovelPortal/appsettings.Development.json @@ -5,5 +5,6 @@ "Default": "Information", "Microsoft.AspNetCore": "Warning" } - } + }, + "WebAPIUrl": "http://seedboxnew:5433/api/" }