.NET for Android

Learn about Sentry's .NET integration with .NET for Android

Sentry supports .NET for Android directly from the Sentry NuGet package.

Add the Sentry dependency to your .NET for Android application:

Copied
dotnet add package Sentry -v 5.7.0

In your MainActivity.cs file, call SentrySdk.Init in the OnCreate event and include any options you would like to set. The Dsn is the only required parameter.

Copied
[Activity(Label = "@string/app_name", MainLauncher = true)]
public class MainActivity : Activity
{
    protected override void OnCreate(Bundle? savedInstanceState)
    {
        SentrySdk.Init(options =>
        {
            options.Dsn = "https://examplePublicKey@o0.ingest.sentry.io/0";
            options.SendDefaultPii = true; // adds the user's IP address automatically
#if DEBUG
            // Log debug information about the Sentry SDK
            options.Debug = true;
#endif

            // Android specific .NET features are under the Android properties:
            options.Android.LogCatIntegration = LogCatIntegrationType.Errors; // Get logcat logs for both handled and unhandled errors; default is unhandled only
            options.Android.LogCatMaxLines = 1000; // Defaults to 1000

            // All the native Android SDK options are available below
            // https://docs.sentry.io/platforms/android/configuration/
            // Enable Native Android SDK ANR detection
            options.Native.AnrEnabled = true;

            // Session Replay is currently available via the ExperimentalOptions
            options.Native.ExperimentalOptions.SessionReplay.OnErrorSampleRate = 1.0;
            options.Native.ExperimentalOptions.SessionReplay.SessionSampleRate = 1.0;
            options.Native.ExperimentalOptions.SessionReplay.MaskAllImages = false;
            options.Native.ExperimentalOptions.SessionReplay.MaskAllText = false;

            options.SetBeforeSend(evt =>
            {
                if (evt.Exception?.Message.Contains("Something you don't care want logged?") ?? false)
                {
                    return null; // return null to filter out event
                }
                // or add additional data
                evt.SetTag("dotnet-Android-Native-Before", "Hello World");
                return evt;
            });
        });

        base.OnCreate(savedInstanceState);
    }
}

The .NET for Android integration is part of Sentry. Please refer to the documentation for that package for information about platform agnostic options.

Android specific options are described below.

Can be set to control whether when LogCat logs are attached to events. It can be set to one of the following values:

  • None: The LogCat integration is disabled.
  • Unhandled: LogCat logs are attached to events only when the event is unhandled.
  • Errors: LogCat logs are attached to events with an exception.
  • All: LogCat logs are attached to all events.

The default is LogCatIntegrationType.None

The maximum number of lines to read from LogCat logs.

The default value is 1000.

This snippet includes an intentional error, so you can test that everything is working as soon as you set it up.

Copied
try
{
    throw null;
}
catch (Exception ex)
{
    SentrySdk.CaptureException(ex);
}
Was this helpful?
Help improve this content
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").