Sentry for Xamarin
Learn more about how Sentry's .NET SDK works with Xamarin applications.
Sentry's .NET SDK works with Xamarin applications through the Sentry Xamarin NuGet package and Xamarin Forms with the Sentry Xamarin Forms NuGet package.
The SDK is compatible with the following versions or higher:
- Xamarin.Android 9.0
- Xamarin.iOS 10.14
- Universal Windows Platform 10.0.16299
- Tizen 4.0
Install the NuGet package:
# For Xamarin.Forms
Install-Package Sentry.Xamarin.Forms -Version 2.1.0
# If you are not using Xamarin.Forms, but only Xamarin:
Install-Package Sentry.Xamarin -Version 2.1.0
# For Xamarin.Forms
Install-Package Sentry.Xamarin.Forms -Version 2.1.0
# If you are not using Xamarin.Forms, but only Xamarin:
Install-Package Sentry.Xamarin -Version 2.1.0
Error Monitoring starts working as soon as you initialize the SDK.
This guide also enables Tracing.
After you've completed setting up a project in Sentry, Sentry will give you a value which we call a DSN or Data Source Name. It looks a lot like a standard URL, but it's just a representation of the configuration required by the Sentry SDK. It consists of a few pieces, including the protocol, public key, the server address, and the project identifier. You should initialize the SDK as early as possible, for an example, the start of OnCreate on MainActivity for Android, and, the top of FinishedLaunching on AppDelegate for iOS).
The snippet also turns on:
- Tracing: via
TracesSampleRate
Don't need a feature? Remove its options from the snippet.
SentryXamarin.Init(options =>
{
options.AddXamarinFormsIntegration();
// Tells which project in Sentry to send events to:
options.Dsn = "___PUBLIC_DSN___";
// When configuring for the first time, to see what the SDK is doing:
options.Debug = true;
// Adds request URL and headers, IP and name for users, etc.
options.SendDefaultPii = true;
// Enables Tracing.
// Set to 1.0 to capture 100% of spans for tracing.
// We recommend reducing this value in production.
options.TracesSampleRate = 1.0;
});
// App code
SentryXamarin.Init(options =>
{
options.AddXamarinFormsIntegration();
// Tells which project in Sentry to send events to:
options.Dsn = "___PUBLIC_DSN___";
// When configuring for the first time, to see what the SDK is doing:
options.Debug = true;
// Adds request URL and headers, IP and name for users, etc.
options.SendDefaultPii = true;
// Enables Tracing.
// Set to 1.0 to capture 100% of spans for tracing.
// We recommend reducing this value in production.
options.TracesSampleRate = 1.0;
});
// App code
The following options are only going to be valid during the SDK initialization.
Disable the native event processor for Android,iOS,UWP
By calling it, the SDK will not automatically Cache your events and they might be lost if a hard crash happens or no connectivity is available during the transport of the event.
Automatically attaches a screenshot of the app at the time of the event capture. By default, it is set to false.
Note
Note that personally identifiable information (PII) might be included in the screenshot when you activate this feature.
Disables the automatic Logging of internal Warnings that comes from Xamarin Forms.
Let's test your setup and confirm that data reaches your Sentry project.
You can verify Sentry is capturing unhandled exceptions by raising an exception. For example, you can use the following snippet to raise a NullReferenceException:
throw null;
throw null;
Open your Sentry project to view the captured event.
For information about Troubleshooting, please visit the troubleshooting page.
- An example using Xamarin Forms and most of the SDK features. (Android, iOS, UWP)
At this point, you should have integrated Sentry into your Xamarin application. Here's what to explore next:
- Explore practical guides on what to monitor, log, track, and investigate after setup
- Learn how to manually capture errors
- Explore Tracing
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").