Skip to content

Commit

Permalink
MAUI events become extra context in Sentry events (#1706)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjohnsonpint authored Jun 13, 2022
1 parent 89cad6d commit bbf2f4a
Show file tree
Hide file tree
Showing 20 changed files with 636 additions and 55 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ docs/docfx/*
docs/docfx.zip
/samples/Sentry.Samples.Aws.Lambda.AspNetCoreServer/Properties/launchSettings.json
*.received.*
mono_crash.*.json
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- Added a new `Sentry.Maui` integration library for the [.NET MAUI](https://dotnet.microsoft.com/apps/maui) platform:
- Initial MAUI support ([#1663](https://github.com/getsentry/sentry-dotnet/pull/1663))
- Continue with adding MAUI support ([#1670](https://github.com/getsentry/sentry-dotnet/pull/1670))
- MAUI events become extra context in Sentry events ([#1706](https://github.com/getsentry/sentry-dotnet/pull/1706))
- Added a new `net6.0-android` target for the `Sentry` core library, which bundles the [Sentry Android SDK](https://docs.sentry.io/platforms/android/):
- Initial .NET 6 Android support ([#1288](https://github.com/getsentry/sentry-dotnet/pull/1288))
- Update Android Support ([#1669](https://github.com/getsentry/sentry-dotnet/pull/1669))
Expand Down
9 changes: 6 additions & 3 deletions Sentry.sln
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,14 @@ EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sentry.DiagnosticSource.Tests", "test\Sentry.DiagnosticSource.Tests\Sentry.DiagnosticSource.Tests.csproj", "{D870B028-16ED-4551-8B0F-5529479D04C9}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sentry.Samples.Android", "samples\Sentry.Samples.Android\Sentry.Samples.Android.csproj", "{5CB9167E-ED23-4A67-8D3A-B66B0C5196C8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sentry.DiagnosticSource.IntegrationTests", "test\Sentry.DiagnosticSource.IntegrationTests\Sentry.DiagnosticSource.IntegrationTests.csproj", "{F8120B9C-D4CA-43DA-B5E1-1CFBA7C36E3B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sentry.Samples.Maui", "samples\Sentry.Samples.Maui\Sentry.Samples.Maui.csproj", "{EBCCABF9-F670-4C8D-AABC-4EB132961929}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sentry.Samples.Maui", "samples\Sentry.Samples.Maui\Sentry.Samples.Maui.csproj", "{EBCCABF9-F670-4C8D-AABC-4EB132961929}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sentry.Maui", "src\Sentry.Maui\Sentry.Maui.csproj", "{FFFC74C5-680B-43E3-9C42-A7A23B589CB6}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sentry.Maui", "src\Sentry.Maui\Sentry.Maui.csproj", "{FFFC74C5-680B-43E3-9C42-A7A23B589CB6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sentry.Maui.Tests", "test\Sentry.Maui.Tests\Sentry.Maui.Tests.csproj", "{143076C0-8D6B-4054-9F45-06B21655F417}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sentry.Maui.Tests", "test\Sentry.Maui.Tests\Sentry.Maui.Tests.csproj", "{143076C0-8D6B-4054-9F45-06B21655F417}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -327,8 +328,10 @@ Global
{F8120B9C-D4CA-43DA-B5E1-1CFBA7C36E3B}.Release|Any CPU.Build.0 = Release|Any CPU
{EBCCABF9-F670-4C8D-AABC-4EB132961929}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EBCCABF9-F670-4C8D-AABC-4EB132961929}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EBCCABF9-F670-4C8D-AABC-4EB132961929}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{EBCCABF9-F670-4C8D-AABC-4EB132961929}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EBCCABF9-F670-4C8D-AABC-4EB132961929}.Release|Any CPU.Build.0 = Release|Any CPU
{EBCCABF9-F670-4C8D-AABC-4EB132961929}.Release|Any CPU.Deploy.0 = Release|Any CPU
{FFFC74C5-680B-43E3-9C42-A7A23B589CB6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FFFC74C5-680B-43E3-9C42-A7A23B589CB6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FFFC74C5-680B-43E3-9C42-A7A23B589CB6}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
15 changes: 11 additions & 4 deletions samples/Sentry.Samples.Maui/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,17 @@
HorizontalOptions="Center" />

<Button
x:Name="ExceptionBtn"
Text="Throw Exception"
SemanticProperties.Hint="Throws an unhandled exception"
Clicked="OnExceptionClicked"
x:Name="ExceptionBtn1"
Text="Throw Unhandled Exception (Crash)"
SemanticProperties.Hint="Throws an unhandled exception, crashing the app"
Clicked="OnUnhandledExceptionClicked"
HorizontalOptions="Center" />

<Button
x:Name="ExceptionBtn2"
Text="Throw and Capture Exception"
SemanticProperties.Hint="Throws an exception and captures it"
Clicked="OnCapturedExceptionClicked"
HorizontalOptions="Center" />

</VerticalStackLayout>
Expand Down
16 changes: 14 additions & 2 deletions samples/Sentry.Samples.Maui/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,21 @@ private void OnCounterClicked(object sender, EventArgs e)
SemanticScreenReader.Announce(CounterBtn.Text);
}

private void OnExceptionClicked(object sebnder, EventArgs e)
private void OnUnhandledExceptionClicked(object sender, EventArgs e)
{
throw new Exception("This is a test exception, thrown from managed code in a MAUI app!");
throw new Exception("This is an unhanded test exception, thrown from managed code in a MAUI app!");
}

private void OnCapturedExceptionClicked(object sender, EventArgs e)
{
try
{
throw new Exception("This is a captured test exception, thrown from managed code in a MAUI app!");
}
catch (Exception ex)
{
SentrySdk.CaptureException(ex);
}
}
}

2 changes: 2 additions & 0 deletions samples/Sentry.Samples.Maui/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public static MauiApp CreateMauiApp() =>
.UseSentry(options =>
{
options.Dsn = "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537";
options.Debug = true;
options.MaxBreadcrumbs = int.MaxValue; // TODO: reduce breadcrumbs, remove this
})
.ConfigureFonts(fonts =>
{
Expand Down
5 changes: 4 additions & 1 deletion samples/Sentry.Samples.Maui/Sentry.Samples.Maui.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@
<!-- Workaround for https://github.com/dotnet/maui/issues/7272 -->
<Target Name="_SetPublishFolderTypeNoneOnDocFileItems" BeforeTargets="_ComputePublishLocation">
<ItemGroup>
<ResolvedFileToPublish Update="@(DocFileItem)" PublishFolderType="None" />
<ResolvedFileToPublish
Update="@(ResolvedFileToPublish)"
Condition="'%(ResolvedFileToPublish.Extension)' == '.xml' And '%(ResolvedFileToPublish.PublishFolderType)' == ''"
PublishFolderType="None" />
</ItemGroup>
</Target>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Reflection;

namespace Sentry.Maui;
namespace Sentry.Maui.Internal;

internal static class Constants
{
Expand Down
23 changes: 23 additions & 0 deletions src/Sentry.Maui/Internal/Disposer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace Sentry.Maui.Internal;

// This is a helper we register as a singleton on the service provider.
// It allows us to register other items that should be disposed when the service provider disposes.
// TODO: There might be something like this built-in to .NET already. Investigate and replace if so.

internal class Disposer : IDisposable
{
private readonly List<IDisposable> _disposables = new();

public void Register(IDisposable disposable)
{
_disposables.Add(disposable);
}

public void Dispose()
{
foreach (var disposable in _disposables)
{
disposable.Dispose();
}
}
}
70 changes: 70 additions & 0 deletions src/Sentry.Maui/Internal/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
namespace Sentry.Maui.Internal;

internal static class Extensions
{
public static void AddBreadcrumbForEvent(this IHub hub,
object? sender,
string eventName,
string? type,
string? category,
Action<Dictionary<string, string>>? addExtraData)
=> hub.AddBreadcrumbForEvent(sender, eventName, type, category, default, addExtraData);

public static void AddBreadcrumbForEvent(this IHub hub,
object? sender,
string eventName,
string? type = null,
string? category = null,
BreadcrumbLevel level = default,
Action<Dictionary<string, string>>? addExtraData = null)
{
var data = new Dictionary<string, string>();
if (sender is Element element)
{
data.AddElementInfo(element, null);
}

addExtraData?.Invoke(data);

var message = sender != null ? $"{sender.GetType().Name}.{eventName}" : eventName;
hub.AddBreadcrumb(message, category, type, data, level);
}

public static void AddElementInfo(this IDictionary<string, string> data, Element? element, string? property)
{
if (element is null)
{
return;
}

var typeName = element.GetType().Name;
var prefix = (property ?? typeName) + ".";

if (property != null)
{
data.Add(property, typeName);
}

// The element ID seems to be mostly useless noise
//data.Add(prefix + nameof(element.Id), element.Id.ToString());

if (element.StyleId != null)
{
// The StyleId correlates to the element's name if one is set in XAML
// TODO: Is there a better way to get this?
data.Add(prefix + "Name", element.StyleId);
}

if (element is ITitledElement { Title: { } } titledElement)
{
// TODO: Scrub PII ?
data.Add(prefix + nameof(titledElement.Title), titledElement.Title);
}

if (element is IText { Text: { } } textElement)
{
// TODO: Scrub PII ?
data.Add(prefix + nameof(textElement.Text), textElement.Text);
}
}
}
6 changes: 6 additions & 0 deletions src/Sentry.Maui/Internal/IMauiEventsBinder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Sentry.Maui.Internal;

internal interface IMauiEventsBinder
{
void BindMauiEvents();
}
Loading

0 comments on commit bbf2f4a

Please sign in to comment.