|
3 | 3 | using Microsoft.Extensions.Hosting; |
4 | 4 | using Microsoft.Extensions.Logging; |
5 | 5 | using NLog.Extensions.Logging; |
6 | | -using System; |
7 | | -using System.IO; |
8 | 6 | using Shared.Logger; |
9 | 7 | using Shared.Middleware; |
| 8 | +using System; |
| 9 | +using System.IO; |
| 10 | +using System.Reflection; |
| 11 | +using Sentry.Extensibility; |
10 | 12 |
|
11 | 13 | namespace MessagingService |
12 | 14 | { |
13 | 15 | using Lamar.Microsoft.DependencyInjection; |
14 | 16 | using NLog; |
| 17 | + using Shared.General; |
15 | 18 | using System.Diagnostics.CodeAnalysis; |
16 | 19 |
|
17 | 20 | [ExcludeFromCodeCoverage] |
@@ -56,6 +59,45 @@ public static IHostBuilder CreateHostBuilder(string[] args) |
56 | 59 |
|
57 | 60 | }).ConfigureWebHostDefaults(webBuilder => |
58 | 61 | { |
| 62 | + webBuilder.ConfigureAppConfiguration((context, configBuilder) => |
| 63 | + { |
| 64 | + var env = context.HostingEnvironment; |
| 65 | + |
| 66 | + configBuilder.SetBasePath(fi.Directory.FullName) |
| 67 | + .AddJsonFile("hosting.json", optional: true) |
| 68 | + .AddJsonFile($"hosting.{env.EnvironmentName}.json", optional: true) |
| 69 | + .AddJsonFile("/home/txnproc/config/appsettings.json", optional: true, reloadOnChange: true) |
| 70 | + .AddJsonFile($"/home/txnproc/config/appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true) |
| 71 | + .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) |
| 72 | + .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true) |
| 73 | + .AddEnvironmentVariables(); |
| 74 | + |
| 75 | + // Build a snapshot of configuration so we can use it immediately (e.g. for Sentry) |
| 76 | + var builtConfig = configBuilder.Build(); |
| 77 | + |
| 78 | + // Keep existing static usage (if you must), and initialise the ConfigurationReader now. |
| 79 | + Startup.Configuration = builtConfig; |
| 80 | + ConfigurationReader.Initialise(Startup.Configuration); |
| 81 | + |
| 82 | + // Configure Sentry on the webBuilder using the config snapshot. |
| 83 | + var sentrySection = builtConfig.GetSection("SentryConfiguration"); |
| 84 | + if (sentrySection.Exists()) |
| 85 | + { |
| 86 | + // Replace the condition below if you intended to only enable Sentry in certain environments. |
| 87 | + if (env.IsDevelopment() == false) |
| 88 | + { |
| 89 | + webBuilder.UseSentry(o => |
| 90 | + { |
| 91 | + o.Dsn = builtConfig["SentryConfiguration:Dsn"]; |
| 92 | + o.SendDefaultPii = true; |
| 93 | + o.MaxRequestBodySize = RequestSize.Always; |
| 94 | + o.CaptureBlockingCalls = true; |
| 95 | + o.Release = Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "unknown"; |
| 96 | + }); |
| 97 | + } |
| 98 | + } |
| 99 | + }); |
| 100 | + |
59 | 101 | webBuilder.UseStartup<Startup>(); |
60 | 102 | webBuilder.UseConfiguration(config); |
61 | 103 | webBuilder.UseKestrel(); |
|
0 commit comments