Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MAUI missing breadcrumbs for lifecycle and UI events #2170

Merged
merged 3 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Fixes

- Fix MAUI missing breadcrumbs for lifecycle and UI events ([#2170](https://github.com/getsentry/sentry-dotnet/pull/2170))

## 3.28.0

### Features
Expand Down
17 changes: 13 additions & 4 deletions src/Sentry.Maui/SentryMauiAppBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Maui.LifecycleEvents;
using Sentry.Extensibility;
using Sentry.Extensions.Logging;
using Sentry.Extensions.Logging.Extensions.DependencyInjection;
using Sentry.Maui;
Expand Down Expand Up @@ -102,14 +103,22 @@ private static void RegisterMauiEventsBinder(this MauiAppBuilder builder)

private static void BindMauiEvents(this IPlatformApplication platformApplication)
{
// Bind to the MAUI application events in a real application control,
// because the required events are not present on the interfaces.
if (platformApplication.Application is not Application application)
// We need to resolve the application manually, because it's not necessarily
// set on platformApplication.Application at this point in the lifecycle.
var services = platformApplication.Services;
var app = services.GetService<IApplication>();

// Use a real Application control, because the required events needed for binding
// are not present on IApplication and related interfaces.
if (app is not Application application)
{
var options = services.GetService<IOptions<SentryMauiOptions>>()?.Value;
options?.LogWarning("Could not bind to MAUI events!");
return;
}

var binder = platformApplication.Services.GetRequiredService<IMauiEventsBinder>();
// Bind the events
var binder = services.GetRequiredService<IMauiEventsBinder>();
binder.BindApplicationEvents(application);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ public partial class SentryMauiAppBuilderExtensionsTests
public void UseSentry_BindsToApplicationStartupEvent_Android()
{
// Arrange
var application = MockApplication.Create();
var binder = Substitute.For<IMauiEventsBinder>();

var builder = _fixture.Builder;
builder.Services.AddSingleton<IApplication>(application);
builder.Services.AddSingleton(binder);
builder.UseSentry(ValidDsn);
using var app = builder.Build();

var application = MockApplication.Create();
var androidApplication = new MockAndroidApplication(application, app.Services);

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ public partial class SentryMauiAppBuilderExtensionsTests
public void UseSentry_BindsToApplicationStartupEvent_iOS()
{
// Arrange
var application = MockApplication.Create();
var binder = Substitute.For<IMauiEventsBinder>();

var builder = _fixture.Builder;
builder.Services.AddSingleton<IApplication>(application);
builder.Services.AddSingleton(binder);
builder.UseSentry(ValidDsn);
using var app = builder.Build();

var application = MockApplication.Create();
var iosApplication = new MockIosApplication(application, app.Services);

// A bit of hackery here, because we can't mock UIKit.UIApplication.
Expand Down