From 98f9d7ef5a61319fdf82e2040d6e18e5e9d5084c Mon Sep 17 00:00:00 2001 From: Stuart Ferguson Date: Sat, 28 Mar 2026 16:53:35 +0000 Subject: [PATCH 1/2] Add Sentry error tracking integration Integrated Sentry.AspNetCore for error monitoring, configuring it via app settings when a DSN is present and not in development. Sentry options such as PII, request body size, and release version are set from configuration. Also reordered and deduplicated using statements, and moved NLog config section for clarity. --- TestHosts/TestHosts/Program.cs | 47 +++++++++++++++++++++------- TestHosts/TestHosts/TestHosts.csproj | 1 + 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/TestHosts/TestHosts/Program.cs b/TestHosts/TestHosts/Program.cs index 7309d1c..9ca0a42 100644 --- a/TestHosts/TestHosts/Program.cs +++ b/TestHosts/TestHosts/Program.cs @@ -6,25 +6,27 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Diagnostics.HealthChecks; using Microsoft.AspNetCore.Hosting; + using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Diagnostics.HealthChecks; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting.WindowsServices; using Microsoft.Extensions.Logging; + using Newtonsoft.Json; + using Newtonsoft.Json.Serialization; using NLog; using NLog.Web; +using Sentry.Extensibility; using Shared.EntityFramework; using Shared.Extensions; using Shared.General; + using Shared.Logger.TennantContext; using Shared.Middleware; using System; using System.IO; +using System.Reflection; using System.Security; - using Microsoft.EntityFrameworkCore; - using Newtonsoft.Json; - using Newtonsoft.Json.Serialization; - using Shared.Logger.TennantContext; using TestHosts; using TestHosts.Common; using TestHosts.Database.PataPawa; @@ -50,10 +52,12 @@ .AddEnvironmentVariables(); ConfigurationReader.Initialise(builder.Configuration); - // ---------------------------------------------------------------------- - // Configure Windows Service mode - // ---------------------------------------------------------------------- - if (WindowsServiceHelpers.IsWindowsService()) + + +// ---------------------------------------------------------------------- +// Configure Windows Service mode +// ---------------------------------------------------------------------- +if (WindowsServiceHelpers.IsWindowsService()) builder.Host.UseWindowsService(); // ---------------------------------------------------------------------- @@ -61,10 +65,29 @@ // ---------------------------------------------------------------------- builder.WebHost.ConfigureKestrel(options => { options.AllowSynchronousIO = true; }); -// ---------------------------------------------------------------------- -// Configure NLog -// ---------------------------------------------------------------------- - string nlogConfigFilename = "nlog.config"; + // Configure Sentry on the webBuilder using the config snapshot. + var sentrySection = ConfigurationReader.GetValueOrDefault("SentryConfiguration", "Dsn", "N/A"); + if (sentrySection != "N/A") + { + // Replace the condition below if you intended to only enable Sentry in certain environments. + if (builder.Environment.IsDevelopment() == false) + { + builder.WebHost.UseSentry(o => + { + o.Dsn = sentrySection; + o.SendDefaultPii = true; + o.MaxRequestBodySize = RequestSize.Always; + o.CaptureBlockingCalls = ConfigurationReader.GetValueOrDefault("SentryConfiguration", "CaptureBlockingCalls", false); + o.IncludeActivityData = ConfigurationReader.GetValueOrDefault("SentryConfiguration", "IncludeActivityData", false); + o.Release = Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "unknown"; + }); + } + } + + // ---------------------------------------------------------------------- + // Configure NLog + // ---------------------------------------------------------------------- + string nlogConfigFilename = "nlog.config"; if (builder.Environment.IsDevelopment()) { String devFile = Path.Combine(builder.Environment.ContentRootPath, "nlog.development.config"); if (File.Exists(devFile)) diff --git a/TestHosts/TestHosts/TestHosts.csproj b/TestHosts/TestHosts/TestHosts.csproj index 963a902..0fe6734 100644 --- a/TestHosts/TestHosts/TestHosts.csproj +++ b/TestHosts/TestHosts/TestHosts.csproj @@ -21,6 +21,7 @@ + From 4aaa55297e198f1fa9473858533016c710dd1416 Mon Sep 17 00:00:00 2001 From: Stuart Ferguson Date: Sat, 28 Mar 2026 16:55:56 +0000 Subject: [PATCH 2/2] ... --- TestHosts/TestHosts/Program.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/TestHosts/TestHosts/Program.cs b/TestHosts/TestHosts/Program.cs index 0e0ba9f..f7b6b2a 100644 --- a/TestHosts/TestHosts/Program.cs +++ b/TestHosts/TestHosts/Program.cs @@ -77,7 +77,8 @@ o.Dsn = sentrySection; o.SendDefaultPii = true; o.MaxRequestBodySize = RequestSize.Always; - o.CaptureBlockingCalls = true; + o.CaptureBlockingCalls = ConfigurationReader.GetValueOrDefault("SentryConfiguration", "CaptureBlockingCalls", false); + o.IncludeActivityData = ConfigurationReader.GetValueOrDefault("SentryConfiguration", "IncludeActivityData", false); o.Release = Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "unknown"; }); }