Skip to content

Commit 756d6f5

Browse files
Update UWP guidance (getsentry#5715)
1 parent 431dee6 commit 756d6f5

File tree

1 file changed

+44
-16
lines changed

1 file changed

+44
-16
lines changed

src/platforms/dotnet/guides/uwp/index.mdx

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,26 @@ Sentry's .NET SDK works with Universal Windows platform applications through the
99

1010
## Configuration
1111

12-
In addition to [initializing the SDK with `SentrySdk.Init`](/platforms/dotnet/), you must configure the UWP specific error handler.
12+
In addition to [initializing the SDK with `SentrySdk.Init`](/platforms/dotnet/), you must configure the UWP specific `Application.UnhandledException` and `Application.Suspending` events.
1313

1414
<Note>
1515

16-
The SDK should be initialized in the constructor of your application class (usually `App.xaml.cs`). Do not initialize the SDK in the `OnLaunched()` event of the application or the `Hub` will not be initialized correctly.
16+
The SDK should be initialized in the constructor of your application class (usually `App.xaml.cs`). Do not initialize the SDK in the `OnLaunched` event of the application or the `Hub` will not be initialized correctly.
1717

1818
</Note>
1919

2020
```csharp
21+
// Add these to your existing using statments.
2122
using Sentry;
22-
using System.Runtime.ExceptionServices;
23-
using System.Security;
24-
using Windows.UI.Xaml;
23+
using Sentry.Protocol;
2524
using UnhandledExceptionEventArgs = Windows.UI.Xaml.UnhandledExceptionEventArgs;
2625

2726
sealed partial class App : Application
2827
{
2928
public App()
3029
{
31-
SentrySdk.Init(o =>
30+
// Initialize Sentry in the App constructor before any other code, to ensure you capture all possible exceptions.
31+
SentrySdk.Init(o =>
3232
{
3333
// Tells which project in Sentry to send events to:
3434
o.Dsn = "___PUBLIC_DSN___";
@@ -40,29 +40,57 @@ sealed partial class App : Application
4040
// We recommend adjusting this value in production.
4141
o.TracesSampleRate = 1.0;
4242

43-
// Enable Global Mode since this is a client app
43+
// Enable Global Mode since this is a client app.
4444
o.IsGlobalModeEnabled = true;
4545

46-
//TODO: any other options you need go here
46+
// TODO:Any other Sentry options you need go here.
4747
});
48-
Current.UnhandledException += ExceptionHandler;
48+
49+
// Hook the UWP UnhandledException event before initalizing the app component.
50+
this.UnhandledException += OnUnhandledException;
51+
52+
// Initialize the app component, and hook the Suspending event.
53+
this.InitializeComponent();
54+
this.Suspending += OnSuspending;
55+
56+
// Add any other code you may need last.
57+
}
58+
59+
// Update your OnSuspending handler as shown
60+
61+
private async void OnSuspending(object sender, SuspendingEventArgs e)
62+
{
63+
var deferral = e.SuspendingOperation.GetDeferral();
64+
65+
// Flush Sentry events when suspending
66+
await SentrySdk.FlushAsync(TimeSpan.FromSeconds(2));
67+
68+
// TODO: Save any other application state and stop any background activity.
69+
70+
deferral.Complete();
4971
}
5072

51-
[HandleProcessCorruptedStateExceptions, SecurityCritical]
52-
internal void ExceptionHandler(object sender, UnhandledExceptionEventArgs e)
73+
// Add this OnUnhandledException handler.
74+
75+
// Use these attributes to ensure all types of exceptions are handled.
76+
[SecurityCritical]
77+
[HandleProcessCorruptedStateExceptions]
78+
private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
5379
{
54-
// We need to hold the reference, because the Exception property is cleared when accessed.
80+
// Get a reference to the exception, because the Exception property is cleared when accessed.
5581
var exception = e.Exception;
5682
if (exception != null)
5783
{
58-
// Tells Sentry this was an Unhandled Exception
84+
// Tell Sentry this was an unhandled exception
5985
exception.Data[Mechanism.HandledKey] = false;
6086
exception.Data[Mechanism.MechanismKey] = "Application.UnhandledException";
87+
88+
// Capture the exception
6189
SentrySdk.CaptureException(exception);
62-
// Make sure the event is flushed to disk or to Sentry
63-
SentrySdk.FlushAsync(TimeSpan.FromSeconds(3)).Wait();
90+
91+
// Flush the event immediately
92+
SentrySdk.FlushAsync(TimeSpan.FromSeconds(2)).GetAwaiter().GetResult();
6493
}
6594
}
66-
6795
}
6896
```

0 commit comments

Comments
 (0)