|
| 1 | +--- |
| 2 | +title: .NET for Android |
| 3 | +sdk: sentry.dotnet.android |
| 4 | +description: "Learn about Sentry's .NET integration with .NET for Android" |
| 5 | +--- |
| 6 | + |
| 7 | +Sentry supports [.NET for Android](https://learn.microsoft.com/dotnet/android/getting-started/installation/) directly from the [Sentry NuGet package](https://www.nuget.org/packages/Sentry). |
| 8 | + |
| 9 | +## Overview of the features |
| 10 | + |
| 11 | +- All the features of our main [.NET SDK](/platforms/dotnet), for your managed code |
| 12 | +- Attach LogCat logs to events |
| 13 | +- Java crash reporting for Android, leveraging our [Android SDK](/platforms/android) including: |
| 14 | + - [Capture Application Not Responding (ANR) errors](/platforms/android/configuration/app-not-respond/) when the app is unresponsive |
| 15 | + - Automatically [attach screenshots to Java exceptions](/platforms/android/enriching-events/screenshots/) |
| 16 | + - Automatic breadcrumbs for Activity lifecycle, App lifecycle, System and Network events and User Interactions |
| 17 | + - Automatic [Activity lifecycle tracing](/platforms/android/tracing/instrumentation/automatic-instrumentation/#activity-instrumentation) and [User Interaction tracing](/platforms/android/performance/instrumentation/automatic-instrumentation/#user-interaction-instrumentation) |
| 18 | + - Device Root checking |
| 19 | +- Native crash reporting for Android, leveraging our [Android NDK](/platforms/android/configuration/using-ndk/) |
| 20 | +- Automatic session tracking and [release health](/product/releases/) |
| 21 | +- _Session Replay for Android_ (currently experimental) |
| 22 | + |
| 23 | +## Install |
| 24 | + |
| 25 | +Add the Sentry dependency to your .NET for Android application: |
| 26 | + |
| 27 | +```shell {tabTitle:.NET Core CLI} |
| 28 | +dotnet add package Sentry.Maui -v {{@inject packages.version('sentry.dotnet') }} |
| 29 | +``` |
| 30 | + |
| 31 | +```powershell {tabTitle:Package Manager} |
| 32 | +Install-Package Sentry.Maui -Version {{@inject packages.version('sentry.dotnet') }} |
| 33 | +``` |
| 34 | + |
| 35 | +## Configure |
| 36 | + |
| 37 | +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. |
| 38 | + |
| 39 | + |
| 40 | +```csharp |
| 41 | +[Activity(Label = "@string/app_name", MainLauncher = true)] |
| 42 | +public class MainActivity : Activity |
| 43 | +{ |
| 44 | + protected override void OnCreate(Bundle? savedInstanceState) |
| 45 | + { |
| 46 | + SentrySdk.Init(options => |
| 47 | + { |
| 48 | + options.Dsn = "___PUBLIC_DSN___"; |
| 49 | + options.SendDefaultPii = true; // adds the user's IP address automatically |
| 50 | +
|
| 51 | + // Android specific .NET features are under the Android properties: |
| 52 | + options.Android.LogCatIntegration = LogCatIntegrationType.Errors; // Get logcat logs for both handled and unhandled errors; default is unhandled only |
| 53 | + options.Android.LogCatMaxLines = 1000; // Defaults to 1000 |
| 54 | +
|
| 55 | + // All the native Android SDK options are available below |
| 56 | + // https://docs.sentry.io/platforms/android/configuration/ |
| 57 | + // Enable Native Android SDK ANR detection |
| 58 | + options.Native.AnrEnabled = true; |
| 59 | + |
| 60 | + // Session Replay is currently available via the ExperimentalOptions |
| 61 | + options.Native.ExperimentalOptions.SessionReplay.OnErrorSampleRate = 1.0; |
| 62 | + options.Native.ExperimentalOptions.SessionReplay.SessionSampleRate = 1.0; |
| 63 | + options.Native.ExperimentalOptions.SessionReplay.MaskAllImages = false; |
| 64 | + options.Native.ExperimentalOptions.SessionReplay.MaskAllText = false; |
| 65 | + |
| 66 | + options.SetBeforeSend(evt => |
| 67 | + { |
| 68 | + if (evt.Exception?.Message.Contains("Something you don't care want logged?") ?? false) |
| 69 | + { |
| 70 | + // return null to filter out an event |
| 71 | + return null; |
| 72 | + } |
| 73 | + // or add additional data |
| 74 | + evt.SetTag("dotnet-Android-Native-Before", "Hello World"); |
| 75 | + return evt; |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + // Here's an example of adding custom scope information. |
| 80 | + // This can be done at any time, and will be passed through to the Native Android SDK as well. |
| 81 | + SentrySdk.ConfigureScope(scope => |
| 82 | + { |
| 83 | + scope.AddBreadcrumb("Custom Breadcrumb"); |
| 84 | + scope.SetExtra("Test", "Custom Extra Data"); |
| 85 | + scope.User = new SentryUser |
| 86 | + { |
| 87 | + Username = "SomeUser", |
| 88 | + |
| 89 | + Other = |
| 90 | + { |
| 91 | + ["CustomInfo"] = "Custom User Info" |
| 92 | + } |
| 93 | + }; |
| 94 | + }); |
| 95 | + |
| 96 | + base.OnCreate(savedInstanceState); |
| 97 | + } |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +### Options |
| 102 | + |
| 103 | +The .NET for Android integration is part of [Sentry](/platforms/dotnet/). Please refer to the documentation for that package for information about platform agnostic options. |
| 104 | + |
| 105 | +Android specific options are described below. |
| 106 | + |
| 107 | +### LogCatIntegration |
| 108 | + |
| 109 | +Can be set to control whether when LogCat logs are attached to events. It can be set to one of the following values: |
| 110 | + |
| 111 | +- `None`: The LogCat integration is disabled. |
| 112 | +- `Unhandled`: LogCat logs are attached to events only when the event is unhandled. |
| 113 | +- `Errors`: LogCat logs are attached to events with an exception. |
| 114 | +- `All`: LogCat logs are attached to all events. |
| 115 | + |
| 116 | +The default is `LogCatIntegrationType.None` |
| 117 | + |
| 118 | +<Alert level="warning"> |
| 119 | + |
| 120 | +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. |
| 121 | + |
| 122 | +</Alert> |
| 123 | + |
| 124 | +### LogCatMaxLines |
| 125 | + |
| 126 | +The maximum number of lines to read from LogCat logs. |
| 127 | + |
| 128 | +The default value is 1000. |
| 129 | + |
| 130 | +## Verify |
| 131 | + |
| 132 | +This snippet includes an intentional error, so you can test that everything is working as soon as you set it up. |
| 133 | + |
| 134 | +<PlatformContent includePath="getting-started-verify" /> |
0 commit comments