Sentry for .NET for iOS, macOS, and Mac Catalyst
Learn about Sentry's .NET integration with .NET for iOS, macOS, and Mac Catalyst
Sentry supports .NET for iOS, macOS, and Mac Catalyst directly from the Sentry NuGet package.
Add the Sentry dependency to your .NET for iOS, macOS, and/or Mac Catalyst application:
dotnet add package Sentry -v 7.0.1
dotnet add package Sentry -v 7.0.1
Install-Package Sentry -Version 7.0.1
Error Monitoring starts working as soon as you call SentrySdk.Init(). Logs sent through SentrySdk.Logger and Application Metrics need no extra configuration. You'll learn how to send them in Send Logs and Send Application Metrics.
This guide also enables Tracing. The iOS and Mac Catalyst setup enables Profiling, and the iOS setup enables experimental Session Replay.
On all three platforms, you get the features of the main .NET SDK for your managed code. On iOS and Mac Catalyst, the embedded Cocoa SDK also provides:
- Native crash reporting, including:
- Automatically attach screenshots to Native exceptions
- Detect App Hangs
- Opt-in automatic performance tracing. See
Native.EnableTracing. - Automatic breadcrumbs
- Swizzling
- Automatic session tracking and release health
In your AppDelegate.cs file, call SentrySdk.Init in FinishedLaunching. Keep your existing window and application setup after the Sentry initialization. The Dsn is the only required setting.
The snippet also turns on:
- Tracing: via
TracesSampleRate - Profiling: via
ProfilesSampleRate. On iOS and Mac Catalyst, the SDK adds the native profiling integration automatically, so you don't need theSentry.Profilingpackage. - Session Replay on iOS: via
Native.ExperimentalOptions.SessionReplay
Don't need a feature? Remove its options from the snippet.
AppDelegate.csusing Foundation;
using Sentry;
using UIKit;
[Register("AppDelegate")]
public class AppDelegate : UIApplicationDelegate
{
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
// Init the Sentry SDK
SentrySdk.Init(options =>
{
options.Dsn = "___PUBLIC_DSN___";
options.SendDefaultPii = true; // adds the user's IP address automatically
// Enables Tracing.
// Set to 1.0 to capture 100% of spans for tracing.
// We recommend reducing this value in production.
options.TracesSampleRate = 1.0;
// Enables Profiling.
// Set to 1.0 to profile 100% of captured transactions.
options.ProfilesSampleRate = 1.0;
#if DEBUG
// Log debug information about the Sentry SDK
options.Debug = true;
#endif
// Native Apple SDK options are available via options.Native
options.Native.EnableAppHangTracking = true;
// If your app doesn't have sensitive data, you can get screenshots on error events automatically
// https://docs.sentry.io/platforms/apple/guides/ios/configuration/options/#attachScreenshot
options.Native.AttachScreenshot = true;
#if IOS
// Session Replay is available on iOS via ExperimentalOptions.
options.Native.ExperimentalOptions.SessionReplay.OnErrorSampleRate = 1.0;
options.Native.ExperimentalOptions.SessionReplay.SessionSampleRate = 1.0;
#endif
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-iOS-Native-Before", "Hello World");
return evt;
});
options.OnCrashedLastRun = e =>
{
Console.WriteLine(e);
};
});
return true;
}
}
using Foundation;
using Sentry;
using UIKit;
[Register("AppDelegate")]
public class AppDelegate : UIApplicationDelegate
{
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
// Init the Sentry SDK
SentrySdk.Init(options =>
{
options.Dsn = "___PUBLIC_DSN___";
options.SendDefaultPii = true; // adds the user's IP address automatically
// Enables Tracing.
// Set to 1.0 to capture 100% of spans for tracing.
// We recommend reducing this value in production.
options.TracesSampleRate = 1.0;
// Enables Profiling.
// Set to 1.0 to profile 100% of captured transactions.
options.ProfilesSampleRate = 1.0;
#if DEBUG
// Log debug information about the Sentry SDK
options.Debug = true;
#endif
// Native Apple SDK options are available via options.Native
options.Native.EnableAppHangTracking = true;
// If your app doesn't have sensitive data, you can get screenshots on error events automatically
// https://docs.sentry.io/platforms/apple/guides/ios/configuration/options/#attachScreenshot
options.Native.AttachScreenshot = true;
#if IOS
// Session Replay is available on iOS via ExperimentalOptions.
options.Native.ExperimentalOptions.SessionReplay.OnErrorSampleRate = 1.0;
options.Native.ExperimentalOptions.SessionReplay.SessionSampleRate = 1.0;
#endif
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-iOS-Native-Before", "Hello World");
return evt;
});
options.OnCrashedLastRun = e =>
{
Console.WriteLine(e);
};
});
return true;
}
}
For a full list of native options, see Apple Native Options.
For an AppKit application targeting net*-macos, initialize Sentry in NSApplicationDelegate.DidFinishLaunching. This target doesn't include the Cocoa integration's options.Native or OnCrashedLastRun APIs.
AppDelegate.csusing AppKit;
using Foundation;
using Sentry;
[Register("AppDelegate")]
public class AppDelegate : NSApplicationDelegate
{
public override void DidFinishLaunching(NSNotification notification)
{
SentrySdk.Init(options =>
{
options.Dsn = "___PUBLIC_DSN___";
options.SendDefaultPii = true;
options.IsGlobalModeEnabled = true;
options.TracesSampleRate = 1.0;
#if DEBUG
options.Debug = true;
#endif
});
// Keep the rest of your application startup here.
}
}
using AppKit;
using Foundation;
using Sentry;
[Register("AppDelegate")]
public class AppDelegate : NSApplicationDelegate
{
public override void DidFinishLaunching(NSNotification notification)
{
SentrySdk.Init(options =>
{
options.Dsn = "___PUBLIC_DSN___";
options.SendDefaultPii = true;
options.IsGlobalModeEnabled = true;
options.TracesSampleRate = 1.0;
#if DEBUG
options.Debug = true;
#endif
});
// Keep the rest of your application startup here.
}
}
For custom transactions, see Verify Your Setup. The Cocoa profiling and replay configuration above doesn't apply to this target.
The .NET for iOS, macOS, and Mac Catalyst integration is part of Sentry. Please refer to the documentation for that package for information about platform agnostic options.
For configuration of the embedded Sentry Cocoa SDK on iOS and Mac Catalyst, see Apple Native Options.
Use SentrySdk.Logger to send logs at different levels. These logs are sent without an additional option.
See Set Up Logs for all available logging methods and formatting options.
SentrySdk.Logger.LogInfo("Order processed");
SentrySdk.Logger.LogWarning("Rate limit reached for /api/orders");
SentrySdk.Logger.LogInfo("Order processed");
SentrySdk.Logger.LogWarning("Rate limit reached for /api/orders");
Metrics are enabled by default in Sentry .NET SDK 6.1.0 and later. Use SentrySdk.Metrics to send counters, gauges, and distributions from your app.
See Set Up Metrics for all available metric types and configuration options.
SentrySdk.Metrics.EmitCounter("button_click", 5,
new Dictionary<string, object> { ["browser"] = "Firefox" });
SentrySdk.Metrics.EmitGauge("page_load", 15.0, MeasurementUnit.Duration.Millisecond,
new Dictionary<string, object> { ["page"] = "/home" });
SentrySdk.Metrics.EmitDistribution("api_latency", 187, MeasurementUnit.Duration.Millisecond,
new Dictionary<string, object> { ["endpoint"] = "/api/orders" });
SentrySdk.Metrics.EmitCounter("button_click", 5,
new Dictionary<string, object> { ["browser"] = "Firefox" });
SentrySdk.Metrics.EmitGauge("page_load", 15.0, MeasurementUnit.Duration.Millisecond,
new Dictionary<string, object> { ["page"] = "/home" });
SentrySdk.Metrics.EmitDistribution("api_latency", 187, MeasurementUnit.Duration.Millisecond,
new Dictionary<string, object> { ["endpoint"] = "/api/orders" });
Let's test your setup and confirm that data reaches your Sentry project.
try
{
throw null;
}
catch (Exception ex)
{
SentrySdk.CaptureException(ex);
}
try
{
throw null;
}
catch (Exception ex)
{
SentrySdk.CaptureException(ex);
}
try
raise <| NullReferenceException ()
with ex ->
SentrySdk.CaptureException(ex) |> ignore
To verify tracing for code without automatic instrumentation, create and finish a test transaction after initializing Sentry:
var transaction = SentrySdk.StartTransaction("quickstart-test", "test");
transaction.Finish();
var transaction = SentrySdk.StartTransaction("quickstart-test", "test");
transaction.Finish();
TracesSampleRate controls sampling; it doesn't create transactions by itself. See Custom Instrumentation to trace work in your application.
If you enabled profiling, run a longer sampled transaction after the profiler has initialized. A short verification transaction might finish before the profiler collects any samples.
Run any log and metric examples you've added after SDK initialization. To verify automatic tracing, exercise an instrumented operation, such as an incoming HTTP request or a supported UI interaction.
Logs and metrics are batched. In an async test method, flush pending data before a short-lived test exits:
await SentrySdk.FlushAsync();
await SentrySdk.FlushAsync();
Check Issues for the captured error, Logs for log messages, Metrics for application metrics, and Traces for sampled transactions. Error capture alone doesn't verify the other features.
At this point, you should have integrated Sentry into your .NET for iOS, macOS, or Mac Catalyst 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
- On iOS and Mac Catalyst, configure the embedded Cocoa SDK with Apple Native Options
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").