.NET for Android
Learn about Sentry's .NET integration with .NET for Android
Sentry supports .NET for Android directly from the Sentry NuGet package.
- All the features of our main .NET SDK, for your managed code
- Attach LogCat logs to events
- Java crash reporting for Android, leveraging our Android SDK including:
- Capture Application Not Responding (ANR) errors when the app is unresponsive
- Automatically attach screenshots to Java exceptions
- Automatic breadcrumbs for Activity lifecycle, App lifecycle, System and Network events and User Interactions
- Automatic Activity lifecycle tracing and User Interaction tracing
- Device Root checking
- Native crash reporting for Android, leveraging our Android NDK
- Automatic session tracking and release health
- Session Replay for Android (currently experimental)
Add the Sentry dependency to your .NET for Android application:
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.
[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
Use caution when enabling LogCatIntegrationType.All
, as this may result in a lot of data being sent to Sentry and performance issues if the SDK generates a lot of events.
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.
try
{
throw null;
}
catch (Exception ex)
{
SentrySdk.CaptureException(ex);
}
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").