Skip to content

Commit 40ec0d8

Browse files
Add and configure options for the iOS SDK (#1849)
1 parent b383c4c commit 40ec0d8

21 files changed

+762
-166
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- Enable Scope Sync for iOS ([#1834](https://github.com/getsentry/sentry-dotnet/pull/1834))
1111
- Add API for deliberately crashing an app ([#1842](https://github.com/getsentry/sentry-dotnet/pull/1842))
1212
- Add Mac Catalyst target ([#1848](https://github.com/getsentry/sentry-dotnet/pull/1848))
13+
- Add and configure options for the iOS SDK ([#1849](https://github.com/getsentry/sentry-dotnet/pull/1849))
1314

1415
### Fixes
1516

samples/Sentry.Samples.Maui/MainPage.xaml.cs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@ public partial class MainPage
88
{
99
private readonly ILogger<MainPage> _logger;
1010

11-
int count = 0;
11+
private int _count = 0;
1212

1313
// NOTE: You can only inject an ILogger<T>, not a plain ILogger
1414
public MainPage(ILogger<MainPage> logger)
15+
{
16+
_logger = logger;
17+
InitializeComponent();
18+
}
19+
20+
protected override void OnAppearing()
1521
{
1622
#if !ANDROID
1723
JavaCrashBtn.IsVisible = false;
@@ -20,22 +26,25 @@ public MainPage(ILogger<MainPage> logger)
2026
#if !(ANDROID || IOS || MACCATALYST)
2127
NativeCrashBtn.IsVisible = false;
2228
#endif
23-
_logger = logger;
24-
InitializeComponent();
29+
base.OnAppearing();
2530
}
2631

2732
private void OnCounterClicked(object sender, EventArgs e)
2833
{
29-
count++;
34+
_count++;
3035

31-
if (count == 1)
32-
CounterBtn.Text = $"Clicked {count} time";
36+
if (_count == 1)
37+
{
38+
CounterBtn.Text = $"Clicked {_count} time";
39+
}
3340
else
34-
CounterBtn.Text = $"Clicked {count} times";
41+
{
42+
CounterBtn.Text = $"Clicked {_count} times";
43+
}
3544

3645
SemanticScreenReader.Announce(CounterBtn.Text);
3746

38-
_logger.LogInformation("The button has been clicked {ClickCount} times", count);
47+
_logger.LogInformation("The button has been clicked {ClickCount} times", _count);
3948
}
4049

4150
private void OnUnhandledExceptionClicked(object sender, EventArgs e)
@@ -47,7 +56,7 @@ private void OnCapturedExceptionClicked(object sender, EventArgs e)
4756
{
4857
try
4958
{
50-
SentrySdk.CauseCrash(CrashType.Managed);
59+
throw new ApplicationException("This exception was thrown and captured manually, without crashing the app.");
5160
}
5261
catch (Exception ex)
5362
{

src/Sentry/Internal/Constants.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ internal static class Constants
3535
// See: https://github.com/getsentry/sentry-release-registry
3636
#if ANDROID
3737
public const string SdkName = "sentry.dotnet.android";
38+
#elif IOS || MACCATALYST
39+
public const string SdkName = "sentry.dotnet.cocoa";
3840
#else
3941
public const string SdkName = "sentry.dotnet";
4042
#endif

src/Sentry/Internal/ProcessInfo.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ internal ProcessInfo(
7474
{
7575
#if ANDROID || IOS || MACCATALYST
7676
options.LogWarning("StartupTimeDetectionMode.Best is not available on this platform. Using 'Fast' mode.");
77-
return;
7877
#else
7978
// StartupTime is set to UtcNow in this constructor.
8079
// That's computationally cheap but not very precise.
@@ -99,7 +98,7 @@ internal ProcessInfo(
9998
}
10099
}
101100

102-
#if !ANDROID
101+
#if !(ANDROID || IOS || MACCATALYST)
103102
private static DateTimeOffset GetStartupTime()
104103
{
105104
using var proc = Process.GetCurrentProcess();

src/Sentry/Platforms/Android/Extensions/MiscExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace Sentry.Android.Extensions;
22

3-
internal static class SMiscExtensions
3+
internal static class MiscExtensions
44
{
55
public static SentryId ToSentryId(this Java.Protocol.SentryId sentryId) => new(Guid.Parse(sentryId.ToString()));
66

src/Sentry/Platforms/Android/SentryOptions.cs

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ internal AndroidOptions() { }
3333
/// <remarks>
3434
/// See https://docs.sentry.io/platforms/android/configuration/app-not-respond/
3535
/// </remarks>
36-
public bool AnrReportInDebug { get; set; }
36+
public bool AnrReportInDebug { get; set; } = false;
3737

3838
/// <summary>
3939
/// Gets or sets the ANR (Application Not Responding) timeout interval.
@@ -53,7 +53,7 @@ internal AndroidOptions() { }
5353
/// This feature is provided by the Sentry Android SDK and thus only works for Java-based errors.
5454
/// See https://docs.sentry.io/platforms/android/enriching-events/screenshots/
5555
/// </remarks>
56-
public bool AttachScreenshot { get; set; }
56+
public bool AttachScreenshot { get; set; } = false;
5757

5858
/// <summary>
5959
/// Gets or sets a value that indicates if automatic breadcrumbs for <c>Activity</c> lifecycle events are
@@ -129,7 +129,7 @@ internal AndroidOptions() { }
129129
/// <remarks>
130130
/// See https://docs.sentry.io/platforms/android/performance/instrumentation/automatic-instrumentation/#user-interaction-instrumentation
131131
/// </remarks>
132-
public bool EnableUserInteractionTracing { get; set; }
132+
public bool EnableUserInteractionTracing { get; set; } = false;
133133

134134
/// <summary>
135135
/// Gets or sets the interval for profiling traces, when enabled with <see cref="ProfilingEnabled"/>.
@@ -144,22 +144,22 @@ internal AndroidOptions() { }
144144
/// Gets or sets a value that indicates if all the threads are automatically attached to all logged events.
145145
/// The default value is <c>false</c> (disabled).
146146
/// </summary>
147-
public bool AttachThreads { get; set; }
147+
public bool AttachThreads { get; set; } = false;
148148

149149
/// <summary>
150150
/// Gets or sets the connection timeout on the HTTP connection used by Java when sending data to Sentry.
151151
/// The default value is 5 seconds.
152152
/// </summary>
153153
public TimeSpan ConnectionTimeout { get; set; } = TimeSpan.FromSeconds(5);
154154

155-
// TODO: Should we have this Distribution property on SentryOptions (with Release and Environment)?
156155
/// <summary>
157-
/// Gets or sets the distribution.
156+
/// Gets or sets the distribution of the application.
158157
/// </summary>
159158
/// <remarks>
160-
/// See https://docs.sentry.io/platforms/java/guides/spring/configuration/#distribution
159+
/// See "dist" in https://develop.sentry.dev/sdk/event-payloads/#optional-attributes
161160
/// </remarks>
162-
public string? Distribution { get; set; }
161+
// TODO: Should we have this property on the main SentryOptions (with Release and Environment)?
162+
public string? Distribution { get; set; } = null;
163163

164164
/// <summary>
165165
/// Gets or sets a value that indicates if the NDK (Android Native Development Kit) is enabled.
@@ -186,14 +186,14 @@ internal AndroidOptions() { }
186186
/// Gets or sets a value that indicates if uncaught Java errors will have their stack traces
187187
/// printed to the standard error stream. The default value is <c>false</c> (disabled).
188188
/// </summary>
189-
public bool PrintUncaughtStackTrace { get; set; }
189+
public bool PrintUncaughtStackTrace { get; set; } = false;
190190

191191
/// <summary>
192192
/// Gets or sets if profiling is enabled for transactions.
193193
/// The default value is <c>false</c> (disabled).
194194
/// See also <see cref="ProfilingTracesInterval"/>.
195195
/// </summary>
196-
public bool ProfilingEnabled { get; set; }
196+
public bool ProfilingEnabled { get; set; } = false;
197197

198198
/// <summary>
199199
/// Gets or sets the read timeout on the HTTP connection used by Java when sending data to Sentry.
@@ -204,8 +204,8 @@ internal AndroidOptions() { }
204204

205205
// ---------- Other ----------
206206

207-
internal string[]? InAppExclude { get; set; }
208-
internal string[]? InAppInclude { get; set; }
207+
internal List<string>? InAppExcludes { get; private set; }
208+
internal List<string>? InAppIncludes { get; private set; }
209209

210210
/// <summary>
211211
/// Add prefix to exclude from 'InApp' stacktrace list by the Android SDK.
@@ -218,10 +218,11 @@ internal AndroidOptions() { }
218218
/// <example>
219219
/// 'java.util.', 'org.apache.logging.log4j.'
220220
/// </example>
221-
public void AddInAppExclude(string prefix) =>
222-
InAppExclude = InAppExclude != null
223-
? InAppExclude.Concat(new[] { prefix }).ToArray()
224-
: new[] { prefix };
221+
public void AddInAppExclude(string prefix)
222+
{
223+
InAppExcludes ??= new List<string>();
224+
InAppExcludes.Add(prefix);
225+
}
225226

226227
/// <summary>
227228
/// Add prefix to include as in 'InApp' stacktrace by the Android SDK.
@@ -234,26 +235,26 @@ public void AddInAppExclude(string prefix) =>
234235
/// <example>
235236
/// 'java.util.customcode.', 'io.sentry.samples.'
236237
/// </example>
237-
public void AddInAppInclude(string prefix) =>
238-
InAppInclude = InAppInclude != null
239-
? InAppInclude.Concat(new[] { prefix }).ToArray()
240-
: new[] { prefix };
238+
public void AddInAppInclude(string prefix){
239+
InAppIncludes ??= new List<string>();
240+
InAppIncludes.Add(prefix);
241+
}
241242

242243
/// <summary>
243244
/// Gets or sets a value that indicates if tracing features are enabled on the embedded Android SDK.
244245
/// The default value is <c>false</c> (disabled).
245246
/// </summary>
246-
public bool EnableAndroidSdkTracing { get; set; }
247+
public bool EnableAndroidSdkTracing { get; set; } = false;
247248

248249
/// <summary>
249250
/// Gets or sets a value that indicates if the <see cref="BeforeSend"/> callback will be invoked for
250251
/// events that originate from the embedded Android SDK. The default value is <c>false</c> (disabled).
251252
/// </summary>
252253
/// <remarks>
253-
/// This is an experimental feature and is imperefct, as the .NET SDK and the embedded Android SDK don't
254+
/// This is an experimental feature and is imperfect, as the .NET SDK and the embedded Android SDK don't
254255
/// implement all of the same features that may be present in the event graph. Some optional elements may
255-
/// be stripped away during the roundtripping between the two SDKs. Use with caution.
256+
/// be stripped away during the round-tripping between the two SDKs. Use with caution.
256257
/// </remarks>
257-
public bool EnableAndroidSdkBeforeSend { get; set; }
258+
public bool EnableAndroidSdkBeforeSend { get; set; } = false;
258259
}
259260
}

src/Sentry/Platforms/Android/SentrySdk.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,8 @@ private static void InitSentryAndroidSdk(SentryOptions options)
9494
o.CacheDirPath = Path.Combine(cacheDirectoryPath, "android");
9595
}
9696

97-
var javaTags = o.Tags;
98-
foreach (var tag in options.DefaultTags)
99-
{
100-
javaTags.Add(tag);
101-
}
97+
// NOTE: Tags in options.DefaultTags should not be passed down, because we already call SetTag on each
98+
// one when sending events, which is relayed through the scope observer.
10299

103100
if (options.HttpProxy is System.Net.WebProxy proxy)
104101
{
@@ -160,8 +157,8 @@ private static void InitSentryAndroidSdk(SentryOptions options)
160157
o.ReadTimeoutMillis = (int)options.Android.ReadTimeout.TotalMilliseconds;
161158

162159
// In-App Excludes and Includes to be passed to the Android SDK
163-
options.Android.InAppExclude?.ToList().ForEach(x => o.AddInAppExclude(x));
164-
options.Android.InAppInclude?.ToList().ForEach(x => o.AddInAppInclude(x));
160+
options.Android.InAppExcludes?.ForEach(x => o.AddInAppExclude(x));
161+
options.Android.InAppIncludes?.ForEach(x => o.AddInAppInclude(x));
165162

166163
// These options are intentionally set and not exposed for modification
167164
o.EnableExternalConfiguration = false;

src/Sentry/Platforms/iOS/Bindings/ApiDefinitions.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ interface Constants
6666
// @interface PrivateSentrySDKOnly : NSObject
6767
[BaseType (typeof(NSObject))]
6868
[Internal]
69-
interface PrivateSentrySDKOnly
69+
interface PrivateSentrySdkOnly
7070
{
7171
// +(void)storeEnvelope:(SentryEnvelope * _Nonnull)envelope;
7272
[Static]
@@ -117,12 +117,12 @@ interface PrivateSentrySDKOnly
117117
// @property (assign, nonatomic, class) BOOL appStartMeasurementHybridSDKMode;
118118
[Static]
119119
[Export ("appStartMeasurementHybridSDKMode")]
120-
bool AppStartMeasurementHybridSDKMode { get; set; }
120+
bool AppStartMeasurementHybridSdkMode { get; set; }
121121

122122
// @property (assign, nonatomic, class) BOOL framesTrackingMeasurementHybridSDKMode;
123123
[Static]
124124
[Export ("framesTrackingMeasurementHybridSDKMode")]
125-
bool FramesTrackingMeasurementHybridSDKMode { get; set; }
125+
bool FramesTrackingMeasurementHybridSdkMode { get; set; }
126126

127127
// @property (readonly, assign, nonatomic, class) BOOL isFramesTrackingRunning;
128128
[Static]
@@ -827,7 +827,7 @@ interface SentryOptions
827827

828828
// @property (copy, nonatomic) SentryBeforeSendEventCallback _Nullable beforeSend;
829829
[NullAllowed, Export ("beforeSend", ArgumentSemantic.Copy)]
830-
Func<SentryEvent, SentryEvent> BeforeSend { get; set; }
830+
Func<SentryEvent?, SentryEvent> BeforeSend { get; set; }
831831

832832
// @property (copy, nonatomic) SentryBeforeBreadcrumbCallback _Nullable beforeBreadcrumb;
833833
[NullAllowed, Export ("beforeBreadcrumb", ArgumentSemantic.Copy)]
@@ -916,7 +916,7 @@ interface SentryOptions
916916

917917
// @property (nonatomic) SentryTracesSamplerCallback _Nullable tracesSampler;
918918
[NullAllowed, Export ("tracesSampler", ArgumentSemantic.Assign)]
919-
Func<SentrySamplingContext, NSNumber> TracesSampler { get; set; }
919+
Func<SentrySamplingContext, NSNumber?> TracesSampler { get; set; }
920920

921921
// @property (readonly, assign, nonatomic) BOOL isTracingEnabled;
922922
[Export ("isTracingEnabled")]
@@ -1392,7 +1392,7 @@ interface SentryNSError : SentrySerializable
13921392
[BaseType (typeof(NSObject))]
13931393
[DisableDefaultCtor]
13941394
[Internal]
1395-
interface SentrySDK
1395+
interface SentrySdk
13961396
{
13971397
// @property (readonly, nonatomic, class) id<SentrySpan> _Nullable span;
13981398
[Static]

0 commit comments

Comments
 (0)