From b71fbf2577e1d40d767ade8219e76a7f4315f3e7 Mon Sep 17 00:00:00 2001 From: StuartFerguson Date: Thu, 19 Mar 2026 20:06:38 +0000 Subject: [PATCH] Add Sentry integration and improve config handling Added Sentry.AspNetCore package and integrated Sentry error tracking based on configuration, excluding development mode. Refactored Program.cs to centralize configuration loading from multiple sources, and simplified Startup.cs to use this configuration. Updated GitHub Actions workflow to pass version info during publish. --- .github/workflows/createrelease.yml | 4 ++ TransactionProcessorACL/Program.cs | 46 ++++++++++++++++++- TransactionProcessorACL/Startup.cs | 8 ---- .../TransactionProcessorACL.csproj | 1 + 4 files changed, 49 insertions(+), 10 deletions(-) diff --git a/.github/workflows/createrelease.yml b/.github/workflows/createrelease.yml index 2e3b7ee..f37c57d 100644 --- a/.github/workflows/createrelease.yml +++ b/.github/workflows/createrelease.yml @@ -48,6 +48,10 @@ jobs: - name: Publish API if: ${{ github.event.release.prerelease == false }} run: dotnet publish "TransactionProcessorACL\TransactionProcessorACL.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 diff --git a/TransactionProcessorACL/Program.cs b/TransactionProcessorACL/Program.cs index 70f196c..2ab2127 100644 --- a/TransactionProcessorACL/Program.cs +++ b/TransactionProcessorACL/Program.cs @@ -2,20 +2,23 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using Shared.Logger; +using Shared.Middleware; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using Shared.Logger; -using Shared.Middleware; namespace TransactionProcessorACL { using Lamar.Microsoft.DependencyInjection; using NLog; using NLog.Extensions.Logging; + using Sentry.Extensibility; + using Shared.General; using System.Diagnostics.CodeAnalysis; using System.IO; + using System.Reflection; [ExcludeFromCodeCoverage] public class Program @@ -60,6 +63,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/TransactionProcessorACL/Startup.cs b/TransactionProcessorACL/Startup.cs index d775861..2e86be0 100644 --- a/TransactionProcessorACL/Startup.cs +++ b/TransactionProcessorACL/Startup.cs @@ -36,14 +36,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(); - - Startup.Configuration = builder.Build(); Startup.WebHostEnvironment = webHostEnvironment; } diff --git a/TransactionProcessorACL/TransactionProcessorACL.csproj b/TransactionProcessorACL/TransactionProcessorACL.csproj index ec5fabd..fbb9d47 100644 --- a/TransactionProcessorACL/TransactionProcessorACL.csproj +++ b/TransactionProcessorACL/TransactionProcessorACL.csproj @@ -36,6 +36,7 @@ +