From d60f5faff51a77e9fda6c26c81741f6de93bbc89 Mon Sep 17 00:00:00 2001 From: Nguyen Quy Hy Date: Tue, 17 Dec 2024 03:35:48 -0500 Subject: [PATCH] Add Sentry --- FlightStreamDeck.AddOn/App.xaml.cs | 39 +++++++++++++++++++ .../FlightStreamDeck.AddOn.csproj | 6 +++ FlightStreamDeck.AddOn/Sentry.txt | 0 3 files changed, 45 insertions(+) create mode 100644 FlightStreamDeck.AddOn/Sentry.txt diff --git a/FlightStreamDeck.AddOn/App.xaml.cs b/FlightStreamDeck.AddOn/App.xaml.cs index f9a22df..2a232ef 100644 --- a/FlightStreamDeck.AddOn/App.xaml.cs +++ b/FlightStreamDeck.AddOn/App.xaml.cs @@ -4,13 +4,16 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using Sentry; using Serilog; using SharpDeck.Connectivity; using SharpDeck.Extensions.Hosting; using System; using System.IO; +using System.Reflection; using System.Threading.Tasks; using System.Windows; +using System.Windows.Threading; namespace FlightStreamDeck.AddOn; @@ -25,6 +28,8 @@ public partial class App : Application protected override void OnStartup(StartupEventArgs e) { + InitializeSentry(); + var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); @@ -48,6 +53,40 @@ protected override void OnStartup(StartupEventArgs e) } } + private void InitializeSentry() + { + if (Assembly.GetExecutingAssembly().GetManifestResourceStream("FlightStreamDeck.AddOn.Sentry.txt") is Stream stream) + { + using (stream) + { + using var reader = new StreamReader(stream); + var dsn = reader.ReadLine(); + if (!string.IsNullOrWhiteSpace(dsn)) + { + DispatcherUnhandledException += App_DispatcherUnhandledException; + SentrySdk.Init(o => + { + // Tells which project in Sentry to send events to: + o.Dsn = dsn; + // When configuring for the first time, to see what the SDK is doing: + //o.Debug = true; + // Set TracesSampleRate to 1.0 to capture 100% of transactions for tracing. + // We recommend adjusting this value in production. + o.TracesSampleRate = 1.0; + }); + } + } + } + } + + void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) + { + SentrySdk.CaptureException(e.Exception); + + // If you want to avoid the application from crashing: + //e.Handled = true; + } + private IHost? InitializeStreamDeckHost(ServiceCollection serviceCollection) { if (Environment.GetCommandLineArgs().Length <= 1) diff --git a/FlightStreamDeck.AddOn/FlightStreamDeck.AddOn.csproj b/FlightStreamDeck.AddOn/FlightStreamDeck.AddOn.csproj index 92881d8..fb5cc0d 100644 --- a/FlightStreamDeck.AddOn/FlightStreamDeck.AddOn.csproj +++ b/FlightStreamDeck.AddOn/FlightStreamDeck.AddOn.csproj @@ -12,6 +12,11 @@ + + + + + @@ -20,6 +25,7 @@ + diff --git a/FlightStreamDeck.AddOn/Sentry.txt b/FlightStreamDeck.AddOn/Sentry.txt new file mode 100644 index 0000000..e69de29