Xamarin

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

Install the NuGet package:

Copied
# For Xamarin.Forms
Install-Package Sentry.Xamarin.Forms -Version 1.2.0

# If you are not using Xamarin.Forms, but only Xamarin:
Install-Package Sentry.Xamarin -Version 1.2.0

Configure

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 SDKs. 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).

Copied
using Sentry;

SentryXamarin.Init(o =>
{
    o.AddXamarinFormsIntegration();
    // Tells which project in Sentry to send events to:
    o.Dsn = "https://examplePublicKey@o0.ingest.sentry.io/0";
    // 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 performance monitoring.
    // We recommend adjusting this value in production
    o.TracesSampleRate = 1.0;
});
// App code

Verify

Great! Now that you’ve completed setting up the SDK, maybe you want to quickly test out how Sentry works. Start by capturing an exception:

You can verify Sentry is capturing unhandled exceptions by raising an exception. For example, you can use the following snippet to raise a NullReferenceException:

Copied
throw null;

All you need to do is to initialize the SDK with SentryXamarin.Init, you will not need any additional changes to get the SDK ready to go.

The SDK automatically handles AppDomain.UnhandledException and Application.UnhandledException, so you'll not need to worry about trying to Catch Unhandled Exceptions.

Xamarin Options

The following options are only going to be valid during the SDK initialization.

DisableNativeIntegration

Disable the native event processor for Android,iOS,UWP

DisableOfflineCaching

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.

Xamarin Forms Options

DisableXamarinWarningsBreadcrumbs

Disables the automatic Logging of internal Warnings that comes from Xamarin Forms.

Troubleshooting

If you need help solving issues with your Sentry's Xamarin SDK, you can read the edge cases documented here. If you need additional help, you can view our forums, and customers on a paid plan may also contact support.

Offline Caching Information

For users on Xamarin iOS, offline caching may cause this tricky issue.

Event/Transaction Capture - Failed to send cached envelope: System.MissingMethodException: Method not found: System.Text.Json.JsonDocument

If this message displays when capturing an event or transaction on your debug window, manually edit your platform csproj:

  1. Close your IDE
  2. Open your iOS .csproj with a text or xml editor
  3. Add the following NuGet reference:
Copied
    <PackageReference Include="System.Memory" IncludeAssets="None">
      <Version>4.5.4</Version>
    </PackageReference>
  1. Save and open your IDE
  2. Wait for the IDE to restore the packages. Once finished, try to capture an exception or transaction

Another way to validate if the problem is happening on an iOS project, is by adding the following code to your iOS project to check if there's a conflict:

Copied
    _ = new byte[2].AsMemory();

Samples

  • An example using Xamarin Forms and most of the SDK features. (Android, iOS, UWP)
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) to suggesting an update ("yeah, this would be better").