Skip to content

Commit afbdd3e

Browse files
Merge pull request #371 from TransactionProcessing/task/#370_sentry_integration
Add Sentry integration and improve config loading
2 parents 64246ad + e18f57a commit afbdd3e

4 files changed

Lines changed: 49 additions & 10 deletions

File tree

.github/workflows/createrelease.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ jobs:
4949
- name: Publish API
5050
if: ${{ github.event.release.prerelease == false }}
5151
run: dotnet publish "MessagingService\MessagingService.csproj" --configuration Release --output publishOutput -r win-x64 --self-contained false
52+
-p:Version=${{ steps.get_version.outputs.VERSION }}
53+
-p:AssemblyVersion=${{ steps.get_version.outputs.VERSION }}
54+
-p:FileVersion=${{ steps.get_version.outputs.VERSION }}
55+
-p:InformationalVersion=${{ steps.get_version.outputs.VERSION }}
5256

5357
- name: Build Release Package
5458
run: |

MessagingService/MessagingService.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="10.1.4" />
3434
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="10.1.4" />
3535
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="10.0.3" />
36+
<PackageReference Include="Sentry.AspNetCore" Version="6.2.0" />
3637
</ItemGroup>
3738

3839
<ItemGroup>

MessagingService/Program.cs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33
using Microsoft.Extensions.Hosting;
44
using Microsoft.Extensions.Logging;
55
using NLog.Extensions.Logging;
6-
using System;
7-
using System.IO;
86
using Shared.Logger;
97
using Shared.Middleware;
8+
using System;
9+
using System.IO;
10+
using System.Reflection;
11+
using Sentry.Extensibility;
1012

1113
namespace MessagingService
1214
{
1315
using Lamar.Microsoft.DependencyInjection;
1416
using NLog;
17+
using Shared.General;
1518
using System.Diagnostics.CodeAnalysis;
1619

1720
[ExcludeFromCodeCoverage]
@@ -56,6 +59,45 @@ public static IHostBuilder CreateHostBuilder(string[] args)
5659

5760
}).ConfigureWebHostDefaults(webBuilder =>
5861
{
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+
59101
webBuilder.UseStartup<Startup>();
60102
webBuilder.UseConfiguration(config);
61103
webBuilder.UseKestrel();

MessagingService/Startup.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,6 @@ public class Startup
3737
{
3838
public Startup(IWebHostEnvironment webHostEnvironment)
3939
{
40-
IConfigurationBuilder builder = new ConfigurationBuilder().SetBasePath(webHostEnvironment.ContentRootPath)
41-
.AddJsonFile("/home/txnproc/config/appsettings.json", true, true)
42-
.AddJsonFile($"/home/txnproc/config/appsettings.{webHostEnvironment.EnvironmentName}.json", optional: true)
43-
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
44-
.AddJsonFile($"appsettings.{webHostEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true)
45-
.AddEnvironmentVariables();
46-
47-
Startup.Configuration = builder.Build();
4840
Startup.WebHostEnvironment = webHostEnvironment;
4941
}
5042

0 commit comments

Comments
 (0)