-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a guide for using Sentry with WinUI 3 (#8557)
- Loading branch information
1 parent
033e26a
commit efa7483
Showing
2 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
title: Windows UI Library |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
--- | ||
title: WinUI | ||
description: "Learn about using Sentry's .NET SDK with the Windows UI Library" | ||
redirect_from: | ||
- /platforms/dotnet/winui/ | ||
--- | ||
|
||
Sentry's .NET SDK works with Windows UI Library applications through the [Sentry NuGet package](https://www.nuget.org/packages/Sentry). | ||
|
||
## Configuration (for Just-In-Time compilation) | ||
|
||
If you are using Just-In-Time (JIT) compilation then all you need to do is [initialize the SDK with `SentrySdk.Init`](/platforms/dotnet/) in the constructor of your application class (usually `App.xaml.cs`). Sentry's integration for WinUI will automatically capture any unhandled exceptions in your application and send these to Sentry. | ||
|
||
<Note> | ||
|
||
Do not initialize the SDK in the `OnLaunched` event of the application or the `Hub` will not be initialized correctly. | ||
|
||
</Note> | ||
|
||
<SignInNote /> | ||
|
||
```csharp | ||
// Add these to your existing using statements. | ||
using Sentry; | ||
using Sentry.Protocol; | ||
|
||
sealed partial class App : Application | ||
{ | ||
public App() | ||
{ | ||
// Initialize Sentry in the App constructor before any other code, to ensure you capture all possible exceptions. | ||
SentrySdk.Init(o => | ||
{ | ||
// Tells which project in Sentry to send events to: | ||
o.Dsn = "___PUBLIC_DSN___"; | ||
|
||
// When configuring for the first time, to see what the SDK is doing: | ||
o.Debug = true; | ||
|
||
// Set traces_sample_rate to 1.0 to capture 100% of transactions for performance monitoring. | ||
// We recommend adjusting this value in production. | ||
o.TracesSampleRate = 1.0; | ||
|
||
// Enable Global Mode since this is a client app. | ||
o.IsGlobalModeEnabled = true; | ||
|
||
// TODO:Any other Sentry options you need go here. | ||
}); | ||
|
||
// Initialize the app component, and hook the Suspending event. | ||
this.InitializeComponent(); | ||
|
||
// Add any other code you may need last. | ||
} | ||
} | ||
``` | ||
|
||
## Configuration (Ahead-of-Time compilation) | ||
|
||
If you are publishing your application using Ahead-of-Time (AOT) compilation or trimming, in addition to [initializing the SDK with `SentrySdk.Init`](/platforms/dotnet/), you must also configure an `Application.UnhandledException` handler in the constructor of your application class (usually `App.xaml.cs`). | ||
|
||
<Note> | ||
|
||
Do not initialize the SDK in the `OnLaunched` event of the application or the `Hub` will not be initialized correctly. | ||
|
||
</Note> | ||
|
||
<SignInNote /> | ||
|
||
```csharp | ||
// Add these to your existing using statements. | ||
using Sentry; | ||
using Sentry.Protocol; | ||
using UnhandledExceptionEventArgs = Microsoft.UI.Xaml.UnhandledExceptionEventArgs; | ||
|
||
sealed partial class App : Application | ||
{ | ||
public App() | ||
{ | ||
// Initialize Sentry in the App constructor before any other code, to ensure you capture all possible exceptions. | ||
SentrySdk.Init(o => | ||
{ | ||
// Tells which project in Sentry to send events to: | ||
o.Dsn = "___PUBLIC_DSN___"; | ||
|
||
// When configuring for the first time, to see what the SDK is doing: | ||
o.Debug = true; | ||
|
||
// Set traces_sample_rate to 1.0 to capture 100% of transactions for performance monitoring. | ||
// We recommend adjusting this value in production. | ||
o.TracesSampleRate = 1.0; | ||
|
||
// Enable Global Mode since this is a client app. | ||
o.IsGlobalModeEnabled = true; | ||
|
||
// Disable Sentry's built in UnhandledException handler as this won't work with AOT compilation | ||
o.DisableWinUiUnhandledExceptionIntegration(); | ||
|
||
// TODO:Any other Sentry options you need go here. | ||
}); | ||
|
||
// Hook the UWP UnhandledException event before initializing the app component. | ||
this.UnhandledException += OnUnhandledException; | ||
|
||
// Initialize the app component, and hook the Suspending event. | ||
this.InitializeComponent(); | ||
|
||
// Add any other code you may need last. | ||
} | ||
|
||
// Add this OnUnhandledException handler. | ||
// Use this attribute to ensure all types of exceptions are handled. | ||
[SecurityCritical] | ||
private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e) | ||
{ | ||
// Get a reference to the exception, because the Exception property is cleared when accessed. | ||
var exception = e.Exception; | ||
if (exception != null) | ||
{ | ||
// Tell Sentry this was an unhandled exception | ||
exception.Data[Mechanism.HandledKey] = false; | ||
exception.Data[Mechanism.MechanismKey] = "Application.UnhandledException"; | ||
|
||
// Capture the exception | ||
SentrySdk.CaptureException(exception); | ||
|
||
// Flush the event immediately | ||
SentrySdk.FlushAsync(TimeSpan.FromSeconds(2)).GetAwaiter().GetResult(); | ||
} | ||
} | ||
} | ||
``` |
efa7483
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
sentry-docs – ./
docs.sentry.io
sentry-docs-git-master.sentry.dev
sentry-docs.sentry.dev