Skip to content

Commit 97e36cb

Browse files
committed
Allow any order for OnAppearing vs. receiving system breadcrumbs
1 parent d3f4476 commit 97e36cb

File tree

3 files changed

+67
-52
lines changed

3 files changed

+67
-52
lines changed

integration-test/net9-maui/App.xaml.cs

Lines changed: 65 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,96 @@ namespace Sentry.Maui.Device.IntegrationTestApp;
77

88
public partial class App : Application
99
{
10-
private static readonly ConcurrentDictionary<string, Breadcrumb> _breadcrumbs = new();
10+
private static readonly ConcurrentDictionary<string, Dictionary<string, string>> systemBreadcrumbs = new();
11+
private static string? testArg;
1112

1213
public App()
1314
{
1415
InitializeComponent();
1516
}
1617

17-
public static string? TestArg => System.Environment.GetEnvironmentVariable("SENTRY_TEST_ARG");
18-
1918
public static bool HasTestArg(string arg)
2019
{
21-
return string.Equals(TestArg, arg, StringComparison.OrdinalIgnoreCase);
20+
return string.Equals(testArg, arg, StringComparison.OrdinalIgnoreCase);
2221
}
2322

24-
public static void RecordBreadcrumb(Breadcrumb breadcrumb)
23+
public static void ReceiveSystemBreadcrumb(Breadcrumb breadcrumb)
2524
{
26-
if (breadcrumb.Data?.TryGetValue("action", out var action) != true)
25+
if (breadcrumb.Type != "system" ||
26+
breadcrumb.Data?.TryGetValue("action", out var action) != true ||
27+
string.IsNullOrEmpty(action))
2728
{
2829
return;
2930
}
3031

31-
_breadcrumbs[action] = new Breadcrumb(
32-
breadcrumb.Message,
33-
breadcrumb.Type,
34-
new Dictionary<string, string>
35-
{
36-
["action"] = action,
37-
["thread_id"] = Thread.CurrentThread.ManagedThreadId.ToString()
38-
},
39-
breadcrumb.Category,
40-
breadcrumb.Level);
32+
systemBreadcrumbs[action] = new Dictionary<string, string>()
33+
{
34+
["action"] = action,
35+
["category"] = breadcrumb.Category ?? string.Empty,
36+
["thread_id"] = Thread.CurrentThread.ManagedThreadId.ToString(),
37+
["type"] = breadcrumb.Type ?? string.Empty,
38+
};
39+
40+
if (HasTestArg(action))
41+
{
42+
// received after OnAppearing
43+
CaptureSystemBreadcrumb(action, systemBreadcrumbs[action]!);
44+
Kill();
45+
}
4146
}
4247

43-
public static bool TryGetBreadcrumb(string action, out Breadcrumb? breadcrumb)
48+
public static void CaptureSystemBreadcrumb(string action, Dictionary<string, string> data)
4449
{
45-
return _breadcrumbs.TryGetValue(action, out breadcrumb);
50+
SentrySdk.CaptureMessage(action, scope =>
51+
{
52+
foreach (var kvp in data)
53+
{
54+
scope.SetExtra(kvp.Key, kvp.Value);
55+
}
56+
});
4657
}
4758

4859
protected override Window CreateWindow(IActivationState? activationState)
4960
{
5061
return new Window(new AppShell());
5162
}
5263

64+
public static void OnAppearing()
65+
{
66+
testArg = System.Environment.GetEnvironmentVariable("SENTRY_TEST_ARG");
67+
68+
#pragma warning disable CS0618
69+
if (Enum.TryParse<CrashType>(testArg, ignoreCase: true, out var crashType))
70+
{
71+
SentrySdk.CauseCrash(crashType);
72+
}
73+
#pragma warning restore CS0618
74+
75+
if (HasTestArg("NullReferenceException"))
76+
{
77+
try
78+
{
79+
object? obj = null;
80+
_ = obj!.ToString();
81+
}
82+
catch (NullReferenceException ex)
83+
{
84+
SentrySdk.CaptureException(ex);
85+
}
86+
Kill();
87+
}
88+
else if (!string.IsNullOrEmpty(testArg) && systemBreadcrumbs.TryGetValue(testArg, out var breadcrumb))
89+
{
90+
// received before OnAppearing
91+
CaptureSystemBreadcrumb(testArg, breadcrumb);
92+
Kill();
93+
}
94+
else if (HasTestArg("None"))
95+
{
96+
Kill();
97+
}
98+
}
99+
53100
public static void Kill()
54101
{
55102
SentrySdk.Close();

integration-test/net9-maui/MainPage.xaml.cs

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,6 @@ protected override void OnAppearing()
1111
{
1212
base.OnAppearing();
1313

14-
#pragma warning disable CS0618
15-
if (Enum.TryParse<CrashType>(App.TestArg, ignoreCase: true, out var crashType))
16-
{
17-
SentrySdk.CauseCrash(crashType);
18-
}
19-
#pragma warning restore CS0618
20-
21-
if (App.HasTestArg("NullReferenceException"))
22-
{
23-
try
24-
{
25-
object? obj = null;
26-
_ = obj!.ToString();
27-
}
28-
catch (NullReferenceException ex)
29-
{
30-
SentrySdk.CaptureException(ex);
31-
}
32-
}
33-
else if (App.TryGetBreadcrumb(App.TestArg, out var breadcrumb) && breadcrumb.Data != null)
34-
{
35-
SentrySdk.CaptureMessage(App.TestArg, scope =>
36-
{
37-
scope.SetExtra("category", breadcrumb.Category);
38-
foreach (var kvp in breadcrumb.Data)
39-
{
40-
scope.SetExtra(kvp.Key, kvp.Value);
41-
}
42-
scope.SetExtra("type", breadcrumb.Type);
43-
});
44-
}
45-
46-
App.Kill();
14+
App.OnAppearing();
4715
}
4816
}

integration-test/net9-maui/MauiProgram.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static MauiApp CreateMauiApp()
2323

2424
options.SetBeforeBreadcrumb((breadcrumb, hint) =>
2525
{
26-
App.RecordBreadcrumb(breadcrumb);
26+
App.ReceiveSystemBreadcrumb(breadcrumb);
2727
return breadcrumb;
2828
});
2929
})

0 commit comments

Comments
 (0)