From bd7330e6287d94b05ab0bbae258f6c78727f9e7d Mon Sep 17 00:00:00 2001 From: Stuart Ferguson Date: Thu, 19 Mar 2026 17:05:26 +0000 Subject: [PATCH] Add Sentry integration and centralize configuration - Integrate Sentry.AspNetCore for error monitoring in non-dev environments, with options loaded from configuration. - Refactor configuration loading to Program.cs using ConfigureAppConfiguration, supporting multiple JSON sources and environment variables. - Remove redundant config loading from Startup.cs. - Update GitHub Actions to set version properties during publish. --- .github/workflows/createrelease.yml | 4 ++ EstateReportingAPI/EstateReportingAPI.csproj | 1 + EstateReportingAPI/Program.cs | 44 +++++++++++++++++++- EstateReportingAPI/Startup.cs | 8 ---- 4 files changed, 48 insertions(+), 9 deletions(-) diff --git a/.github/workflows/createrelease.yml b/.github/workflows/createrelease.yml index 84dc9c3..be8d817 100644 --- a/.github/workflows/createrelease.yml +++ b/.github/workflows/createrelease.yml @@ -47,6 +47,10 @@ jobs: - name: Publish API if: ${{ github.event.release.prerelease == false }} run: dotnet publish "EstateReportingAPI\EstateReportingAPI.csproj" --configuration Release --output publishOutput -r win-x64 --self-contained + -p:Version=${{ steps.get_version.outputs.VERSION }} + -p:AssemblyVersion=${{ steps.get_version.outputs.VERSION }} + -p:FileVersion=${{ steps.get_version.outputs.VERSION }} + -p:InformationalVersion=${{ steps.get_version.outputs.VERSION }} - name: Build Release Package run: | diff --git a/EstateReportingAPI/EstateReportingAPI.csproj b/EstateReportingAPI/EstateReportingAPI.csproj index 48a32f3..12a5dfb 100644 --- a/EstateReportingAPI/EstateReportingAPI.csproj +++ b/EstateReportingAPI/EstateReportingAPI.csproj @@ -36,6 +36,7 @@ + diff --git a/EstateReportingAPI/Program.cs b/EstateReportingAPI/Program.cs index a120cf7..1230176 100644 --- a/EstateReportingAPI/Program.cs +++ b/EstateReportingAPI/Program.cs @@ -1,9 +1,12 @@ using Lamar.Microsoft.DependencyInjection; using NLog; -using System.Diagnostics.CodeAnalysis; using NLog.Extensions.Logging; +using Sentry.Extensibility; +using Shared.General; using Shared.Logger; using Shared.Middleware; +using System.Diagnostics.CodeAnalysis; +using System.Reflection; namespace EstateReportingAPI; @@ -51,6 +54,45 @@ public static IHostBuilder CreateHostBuilder(string[] args) }); hostBuilder.ConfigureWebHostDefaults(webBuilder => { + webBuilder.ConfigureAppConfiguration((context, configBuilder) => + { + var env = context.HostingEnvironment; + + configBuilder.SetBasePath(fi.Directory.FullName) + .AddJsonFile("hosting.json", optional: true) + .AddJsonFile($"hosting.{env.EnvironmentName}.json", optional: true) + .AddJsonFile("/home/txnproc/config/appsettings.json", optional: true, reloadOnChange: true) + .AddJsonFile($"/home/txnproc/config/appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true) + .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) + .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true) + .AddEnvironmentVariables(); + + // Build a snapshot of configuration so we can use it immediately (e.g. for Sentry) + var builtConfig = configBuilder.Build(); + + // Keep existing static usage (if you must), and initialise the ConfigurationReader now. + Startup.Configuration = builtConfig; + ConfigurationReader.Initialise(Startup.Configuration); + + // Configure Sentry on the webBuilder using the config snapshot. + var sentrySection = builtConfig.GetSection("SentryConfiguration"); + if (sentrySection.Exists()) + { + // Replace the condition below if you intended to only enable Sentry in certain environments. + if (env.IsDevelopment() == false) + { + webBuilder.UseSentry(o => + { + o.Dsn = builtConfig["SentryConfiguration:Dsn"]; + o.SendDefaultPii = true; + o.MaxRequestBodySize = RequestSize.Always; + o.CaptureBlockingCalls = true; + o.Release = Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "unknown"; + }); + } + } + }); + webBuilder.UseStartup(); webBuilder.UseConfiguration(config); webBuilder.UseKestrel(); diff --git a/EstateReportingAPI/Startup.cs b/EstateReportingAPI/Startup.cs index d6f5b22..ac70f2a 100644 --- a/EstateReportingAPI/Startup.cs +++ b/EstateReportingAPI/Startup.cs @@ -22,14 +22,6 @@ public class Startup public Startup(IWebHostEnvironment webHostEnvironment) { - IConfigurationBuilder builder = new ConfigurationBuilder().SetBasePath(webHostEnvironment.ContentRootPath) - .AddJsonFile("/home/txnproc/config/appsettings.json", true, true) - .AddJsonFile($"/home/txnproc/config/appsettings.{webHostEnvironment.EnvironmentName}.json", optional: true) - .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) - .AddJsonFile($"appsettings.{webHostEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true) - .AddEnvironmentVariables(); - - Configuration = builder.Build(); WebHostEnvironment = webHostEnvironment; }