From 3e03211efe7a7bc090bc465c0a1164ff66cb5f3b Mon Sep 17 00:00:00 2001 From: RobinTTY Date: Sat, 28 Sep 2024 22:02:56 +0200 Subject: [PATCH] Integrate Serilog and Seq. Resolves #72 --- .../Program.cs | 13 +++++++--- ...binTTY.PersonalFinanceDashboard.API.csproj | 2 ++ .../appsettings.Development.json | 26 ++++++++++++++++--- .../BankingInstitutionRepository.cs | 18 +++++++++---- 4 files changed, 47 insertions(+), 12 deletions(-) diff --git a/src/server/RobinTTY.PersonalFinanceDashboard.API/Program.cs b/src/server/RobinTTY.PersonalFinanceDashboard.API/Program.cs index 9e1dd99..68c0458 100644 --- a/src/server/RobinTTY.PersonalFinanceDashboard.API/Program.cs +++ b/src/server/RobinTTY.PersonalFinanceDashboard.API/Program.cs @@ -20,7 +20,13 @@ var builder = WebApplication.CreateBuilder(args); var appConfig = AppConfigurationManager.AppConfiguration; -// HTTP Setup +// Configure logging +builder.Host.UseSerilog((context, loggerConfig) => +{ + loggerConfig.ReadFrom.Configuration(context.Configuration); +}); + +// HTTP setup builder.Services .AddHttpClient() .AddCors(options => @@ -29,7 +35,7 @@ options.AddDefaultPolicy(policy => policy.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod()); }); -// DB Setup +// DB setup // TODO: The filepath shouldn't be hardcoded builder.Services.AddDbContextPool(options => options.UseSqlite("Data Source=../RobinTTY.PersonalFinanceDashboard.Infrastructure/application.db")); @@ -63,7 +69,7 @@ // - Since the authentication/retrieval logic will differ from provider to provider, it will be difficult // to abstract this logic away into one unified interface, so maybe I will need to refine/scrap this idea later... -// HotChocolate GraphQL Setup +// HotChocolate GraphQL setup builder.Services // TODO: Configure cost analyzer at some point (enforces maximum query costs) .AddGraphQLServer(disableCostAnalyzer: true) @@ -82,6 +88,7 @@ db.Database.Migrate(); } +app.UseSerilogRequestLogging(); app.UseCors(); app.MapGraphQL(); app.Run(); diff --git a/src/server/RobinTTY.PersonalFinanceDashboard.API/RobinTTY.PersonalFinanceDashboard.API.csproj b/src/server/RobinTTY.PersonalFinanceDashboard.API/RobinTTY.PersonalFinanceDashboard.API.csproj index 1843ffb..8ac6ee4 100644 --- a/src/server/RobinTTY.PersonalFinanceDashboard.API/RobinTTY.PersonalFinanceDashboard.API.csproj +++ b/src/server/RobinTTY.PersonalFinanceDashboard.API/RobinTTY.PersonalFinanceDashboard.API.csproj @@ -23,8 +23,10 @@ + + diff --git a/src/server/RobinTTY.PersonalFinanceDashboard.API/appsettings.Development.json b/src/server/RobinTTY.PersonalFinanceDashboard.API/appsettings.Development.json index 0c208ae..05c7fc9 100644 --- a/src/server/RobinTTY.PersonalFinanceDashboard.API/appsettings.Development.json +++ b/src/server/RobinTTY.PersonalFinanceDashboard.API/appsettings.Development.json @@ -1,8 +1,26 @@ { - "Logging": { - "LogLevel": { + "Serilog": { + "Using": [ + "Serilog.Sinks.Console", + "Serilog.Sinks.Seq" + ], + "MinimumLevel": { "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } + "Override": { + "Microsoft.AspNetCore": "Warning" + } + }, + "WriteTo": [ + { + "Name": "Console" + }, + { + "Name": "Seq", + "Args": { + "serverUrl": "http://localhost:5341" + } + } + ], + "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ] } } diff --git a/src/server/RobinTTY.PersonalFinanceDashboard.Infrastructure/Repositories/BankingInstitutionRepository.cs b/src/server/RobinTTY.PersonalFinanceDashboard.Infrastructure/Repositories/BankingInstitutionRepository.cs index 9a73ae9..c12e8c4 100644 --- a/src/server/RobinTTY.PersonalFinanceDashboard.Infrastructure/Repositories/BankingInstitutionRepository.cs +++ b/src/server/RobinTTY.PersonalFinanceDashboard.Infrastructure/Repositories/BankingInstitutionRepository.cs @@ -1,4 +1,5 @@ using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; using RobinTTY.PersonalFinanceDashboard.Core.Models; using RobinTTY.PersonalFinanceDashboard.Infrastructure.Mappers; using RobinTTY.PersonalFinanceDashboard.Infrastructure.Services; @@ -12,6 +13,7 @@ namespace RobinTTY.PersonalFinanceDashboard.Infrastructure.Repositories; /// public class BankingInstitutionRepository { + private readonly ILogger _logger; private readonly ApplicationDbContext _dbContext; private readonly GoCardlessDataProvider _dataProvider; private readonly BankingInstitutionMapper _bankingInstitutionMapper; @@ -20,14 +22,19 @@ public class BankingInstitutionRepository /// /// Creates a new instance of . /// + /// Logger used for monitoring purposes. /// The to use for data retrieval. /// The data provider to use for data retrieval. /// The mapper used to map ef entities to the domain model. /// TODO - public BankingInstitutionRepository(ApplicationDbContext dbContext, GoCardlessDataProvider dataProvider, + public BankingInstitutionRepository( + ILogger logger, + ApplicationDbContext dbContext, + GoCardlessDataProvider dataProvider, BankingInstitutionMapper bankingInstitutionMapper, ThirdPartyDataRetrievalMetadataService dataRetrievalMetadataService) { + _logger = logger; _dbContext = dbContext; _dataProvider = dataProvider; _bankingInstitutionMapper = bankingInstitutionMapper; @@ -72,7 +79,7 @@ public async Task> GetBankingInstitutions(string return bankingInstitutionModels; } - + /// /// TODO /// @@ -110,9 +117,10 @@ private async Task RefreshBankingInstitutionsIfStale() await DeleteBankingInstitutions(); await AddBankingInstitutions(response.Result); await _dataRetrievalMetadataService.ResetDataExpiry(ThirdPartyDataType.BankingInstitutions); - - // TODO: Replace with logger - Console.WriteLine("Banking institutions have been refreshed."); + + _logger.LogInformation( + "Refreshed stale banking institution data. {updateRecords} records were updated.", + response.Result.Count()); } else {