Skip to content

Commit bbf2f4a

Browse files
MAUI events become extra context in Sentry events (#1706)
1 parent 89cad6d commit bbf2f4a

20 files changed

+636
-55
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ docs/docfx/*
2020
docs/docfx.zip
2121
/samples/Sentry.Samples.Aws.Lambda.AspNetCoreServer/Properties/launchSettings.json
2222
*.received.*
23+
mono_crash.*.json

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
- Added a new `Sentry.Maui` integration library for the [.NET MAUI](https://dotnet.microsoft.com/apps/maui) platform:
2020
- Initial MAUI support ([#1663](https://github.com/getsentry/sentry-dotnet/pull/1663))
2121
- Continue with adding MAUI support ([#1670](https://github.com/getsentry/sentry-dotnet/pull/1670))
22+
- MAUI events become extra context in Sentry events ([#1706](https://github.com/getsentry/sentry-dotnet/pull/1706))
2223
- Added a new `net6.0-android` target for the `Sentry` core library, which bundles the [Sentry Android SDK](https://docs.sentry.io/platforms/android/):
2324
- Initial .NET 6 Android support ([#1288](https://github.com/getsentry/sentry-dotnet/pull/1288))
2425
- Update Android Support ([#1669](https://github.com/getsentry/sentry-dotnet/pull/1669))

Sentry.sln

+6-3
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,14 @@ EndProject
137137
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sentry.DiagnosticSource.Tests", "test\Sentry.DiagnosticSource.Tests\Sentry.DiagnosticSource.Tests.csproj", "{D870B028-16ED-4551-8B0F-5529479D04C9}"
138138
EndProject
139139
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sentry.Samples.Android", "samples\Sentry.Samples.Android\Sentry.Samples.Android.csproj", "{5CB9167E-ED23-4A67-8D3A-B66B0C5196C8}"
140+
EndProject
140141
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sentry.DiagnosticSource.IntegrationTests", "test\Sentry.DiagnosticSource.IntegrationTests\Sentry.DiagnosticSource.IntegrationTests.csproj", "{F8120B9C-D4CA-43DA-B5E1-1CFBA7C36E3B}"
141142
EndProject
142-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sentry.Samples.Maui", "samples\Sentry.Samples.Maui\Sentry.Samples.Maui.csproj", "{EBCCABF9-F670-4C8D-AABC-4EB132961929}"
143+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sentry.Samples.Maui", "samples\Sentry.Samples.Maui\Sentry.Samples.Maui.csproj", "{EBCCABF9-F670-4C8D-AABC-4EB132961929}"
143144
EndProject
144-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sentry.Maui", "src\Sentry.Maui\Sentry.Maui.csproj", "{FFFC74C5-680B-43E3-9C42-A7A23B589CB6}"
145+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sentry.Maui", "src\Sentry.Maui\Sentry.Maui.csproj", "{FFFC74C5-680B-43E3-9C42-A7A23B589CB6}"
145146
EndProject
146-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sentry.Maui.Tests", "test\Sentry.Maui.Tests\Sentry.Maui.Tests.csproj", "{143076C0-8D6B-4054-9F45-06B21655F417}"
147+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sentry.Maui.Tests", "test\Sentry.Maui.Tests\Sentry.Maui.Tests.csproj", "{143076C0-8D6B-4054-9F45-06B21655F417}"
147148
EndProject
148149
Global
149150
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -327,8 +328,10 @@ Global
327328
{F8120B9C-D4CA-43DA-B5E1-1CFBA7C36E3B}.Release|Any CPU.Build.0 = Release|Any CPU
328329
{EBCCABF9-F670-4C8D-AABC-4EB132961929}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
329330
{EBCCABF9-F670-4C8D-AABC-4EB132961929}.Debug|Any CPU.Build.0 = Debug|Any CPU
331+
{EBCCABF9-F670-4C8D-AABC-4EB132961929}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
330332
{EBCCABF9-F670-4C8D-AABC-4EB132961929}.Release|Any CPU.ActiveCfg = Release|Any CPU
331333
{EBCCABF9-F670-4C8D-AABC-4EB132961929}.Release|Any CPU.Build.0 = Release|Any CPU
334+
{EBCCABF9-F670-4C8D-AABC-4EB132961929}.Release|Any CPU.Deploy.0 = Release|Any CPU
332335
{FFFC74C5-680B-43E3-9C42-A7A23B589CB6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
333336
{FFFC74C5-680B-43E3-9C42-A7A23B589CB6}.Debug|Any CPU.Build.0 = Debug|Any CPU
334337
{FFFC74C5-680B-43E3-9C42-A7A23B589CB6}.Release|Any CPU.ActiveCfg = Release|Any CPU

samples/Sentry.Samples.Maui/MainPage.xaml

+11-4
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,17 @@
3636
HorizontalOptions="Center" />
3737

3838
<Button
39-
x:Name="ExceptionBtn"
40-
Text="Throw Exception"
41-
SemanticProperties.Hint="Throws an unhandled exception"
42-
Clicked="OnExceptionClicked"
39+
x:Name="ExceptionBtn1"
40+
Text="Throw Unhandled Exception (Crash)"
41+
SemanticProperties.Hint="Throws an unhandled exception, crashing the app"
42+
Clicked="OnUnhandledExceptionClicked"
43+
HorizontalOptions="Center" />
44+
45+
<Button
46+
x:Name="ExceptionBtn2"
47+
Text="Throw and Capture Exception"
48+
SemanticProperties.Hint="Throws an exception and captures it"
49+
Clicked="OnCapturedExceptionClicked"
4350
HorizontalOptions="Center" />
4451

4552
</VerticalStackLayout>

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

+14-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,21 @@ private void OnCounterClicked(object sender, EventArgs e)
2121
SemanticScreenReader.Announce(CounterBtn.Text);
2222
}
2323

24-
private void OnExceptionClicked(object sebnder, EventArgs e)
24+
private void OnUnhandledExceptionClicked(object sender, EventArgs e)
2525
{
26-
throw new Exception("This is a test exception, thrown from managed code in a MAUI app!");
26+
throw new Exception("This is an unhanded test exception, thrown from managed code in a MAUI app!");
27+
}
28+
29+
private void OnCapturedExceptionClicked(object sender, EventArgs e)
30+
{
31+
try
32+
{
33+
throw new Exception("This is a captured test exception, thrown from managed code in a MAUI app!");
34+
}
35+
catch (Exception ex)
36+
{
37+
SentrySdk.CaptureException(ex);
38+
}
2739
}
2840
}
2941

samples/Sentry.Samples.Maui/MauiProgram.cs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ public static MauiApp CreateMauiApp() =>
88
.UseSentry(options =>
99
{
1010
options.Dsn = "https://[email protected]/5428537";
11+
options.Debug = true;
12+
options.MaxBreadcrumbs = int.MaxValue; // TODO: reduce breadcrumbs, remove this
1113
})
1214
.ConfigureFonts(fonts =>
1315
{

samples/Sentry.Samples.Maui/Sentry.Samples.Maui.csproj

+4-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@
6363
<!-- Workaround for https://github.com/dotnet/maui/issues/7272 -->
6464
<Target Name="_SetPublishFolderTypeNoneOnDocFileItems" BeforeTargets="_ComputePublishLocation">
6565
<ItemGroup>
66-
<ResolvedFileToPublish Update="@(DocFileItem)" PublishFolderType="None" />
66+
<ResolvedFileToPublish
67+
Update="@(ResolvedFileToPublish)"
68+
Condition="'%(ResolvedFileToPublish.Extension)' == '.xml' And '%(ResolvedFileToPublish.PublishFolderType)' == ''"
69+
PublishFolderType="None" />
6770
</ItemGroup>
6871
</Target>
6972

src/Sentry.Maui/Constants.cs renamed to src/Sentry.Maui/Internal/Constants.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System.Reflection;
22

3-
namespace Sentry.Maui;
3+
namespace Sentry.Maui.Internal;
44

55
internal static class Constants
66
{

src/Sentry.Maui/Internal/Disposer.cs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace Sentry.Maui.Internal;
2+
3+
// This is a helper we register as a singleton on the service provider.
4+
// It allows us to register other items that should be disposed when the service provider disposes.
5+
// TODO: There might be something like this built-in to .NET already. Investigate and replace if so.
6+
7+
internal class Disposer : IDisposable
8+
{
9+
private readonly List<IDisposable> _disposables = new();
10+
11+
public void Register(IDisposable disposable)
12+
{
13+
_disposables.Add(disposable);
14+
}
15+
16+
public void Dispose()
17+
{
18+
foreach (var disposable in _disposables)
19+
{
20+
disposable.Dispose();
21+
}
22+
}
23+
}
+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
namespace Sentry.Maui.Internal;
2+
3+
internal static class Extensions
4+
{
5+
public static void AddBreadcrumbForEvent(this IHub hub,
6+
object? sender,
7+
string eventName,
8+
string? type,
9+
string? category,
10+
Action<Dictionary<string, string>>? addExtraData)
11+
=> hub.AddBreadcrumbForEvent(sender, eventName, type, category, default, addExtraData);
12+
13+
public static void AddBreadcrumbForEvent(this IHub hub,
14+
object? sender,
15+
string eventName,
16+
string? type = null,
17+
string? category = null,
18+
BreadcrumbLevel level = default,
19+
Action<Dictionary<string, string>>? addExtraData = null)
20+
{
21+
var data = new Dictionary<string, string>();
22+
if (sender is Element element)
23+
{
24+
data.AddElementInfo(element, null);
25+
}
26+
27+
addExtraData?.Invoke(data);
28+
29+
var message = sender != null ? $"{sender.GetType().Name}.{eventName}" : eventName;
30+
hub.AddBreadcrumb(message, category, type, data, level);
31+
}
32+
33+
public static void AddElementInfo(this IDictionary<string, string> data, Element? element, string? property)
34+
{
35+
if (element is null)
36+
{
37+
return;
38+
}
39+
40+
var typeName = element.GetType().Name;
41+
var prefix = (property ?? typeName) + ".";
42+
43+
if (property != null)
44+
{
45+
data.Add(property, typeName);
46+
}
47+
48+
// The element ID seems to be mostly useless noise
49+
//data.Add(prefix + nameof(element.Id), element.Id.ToString());
50+
51+
if (element.StyleId != null)
52+
{
53+
// The StyleId correlates to the element's name if one is set in XAML
54+
// TODO: Is there a better way to get this?
55+
data.Add(prefix + "Name", element.StyleId);
56+
}
57+
58+
if (element is ITitledElement { Title: { } } titledElement)
59+
{
60+
// TODO: Scrub PII ?
61+
data.Add(prefix + nameof(titledElement.Title), titledElement.Title);
62+
}
63+
64+
if (element is IText { Text: { } } textElement)
65+
{
66+
// TODO: Scrub PII ?
67+
data.Add(prefix + nameof(textElement.Text), textElement.Text);
68+
}
69+
}
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Sentry.Maui.Internal;
2+
3+
internal interface IMauiEventsBinder
4+
{
5+
void BindMauiEvents();
6+
}

0 commit comments

Comments
 (0)