Skip to content

Commit 8f8f7de

Browse files
Merge pull request #50 from ministryofjustice/feature/add-sentry
Adds Sentry Serilog sink
2 parents 1220bc3 + 34a3e66 commit 8f8f7de

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/Libraries/EnvironmentSetup/DmsServiceExtensions.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.Extensions.Configuration;
33
using Microsoft.Extensions.DependencyInjection;
44
using Microsoft.Extensions.Hosting;
5+
using Sentry;
56
using Serilog;
67

78
namespace EnvironmentSetup;
@@ -12,12 +13,29 @@ public static class DmsServiceExtensions
1213
/// Configures Serilog logging for all DMS applications.
1314
/// Works for both ASP.NET Core apps and Worker Services.
1415
/// Reads configuration from appsettings.json Serilog section.
16+
/// Integrates with Sentry for error tracking.
1517
/// </summary>
1618
public static IHostApplicationBuilder UseDmsSerilog(this IHostApplicationBuilder builder)
1719
{
20+
var sentryDsn = builder.Configuration["Sentry:Dsn"];
21+
22+
if (!string.IsNullOrWhiteSpace(sentryDsn))
23+
{
24+
SentrySdk.Init(options =>
25+
{
26+
options.Dsn = sentryDsn;
27+
options.Environment = builder.Environment.EnvironmentName;
28+
options.TracesSampleRate = 1.0;
29+
options.AttachStacktrace = true;
30+
options.SendDefaultPii = false;
31+
options.AutoSessionTracking = true;
32+
});
33+
}
34+
1835
builder.Services.AddSerilog(config => config
1936
.ReadFrom.Configuration(builder.Configuration)
20-
.WriteTo.File(Path.Combine("logs", "fatal.txt"), Serilog.Events.LogEventLevel.Fatal));
37+
.WriteTo.File(Path.Combine("logs", "fatal.txt"), Serilog.Events.LogEventLevel.Fatal)
38+
.WriteTo.Sentry());
2139

2240
return builder;
2341
}

0 commit comments

Comments
 (0)