Skip to content

Continue with adding MAUI support #1670

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

Merged
merged 7 commits into from
May 25, 2022
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

### Features

- Initial support for .NET MAUI ([#1663](https://github.com/getsentry/sentry-dotnet/pull/1663))
- Initial support for .NET MAUI ([#1663](https://github.com/getsentry/sentry-dotnet/pull/1663)) ([#1670](https://github.com/getsentry/sentry-dotnet/pull/1670))
- Initial support for `net6.0-android` apps ([#1288](https://github.com/getsentry/sentry-dotnet/pull/1288)) ([#1669](https://github.com/getsentry/sentry-dotnet/pull/1669))

### Fixes
Expand Down
14 changes: 14 additions & 0 deletions samples/Sentry.Samples.Maui/Sentry.Samples.Maui.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>
<TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>

<!--
This avoids attaching a debugger when we crash the app when targeting Windows while running in Visual Studio.
It is not strictly necessary, but makes the demo sligtly cleaner.
-->
<DefineConstants>DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION</DefineConstants>

</PropertyGroup>

<ItemGroup>
Expand All @@ -53,4 +60,11 @@
<ProjectReference Include="..\..\src\Sentry.Maui\Sentry.Maui.csproj" />
</ItemGroup>

<!-- Workaround for https://github.com/dotnet/maui/issues/7272 -->
<Target Name="_SetPublishFolderTypeNoneOnDocFileItems" BeforeTargets="_ComputePublishLocation">
<ItemGroup>
<ResolvedFileToPublish Update="@(DocFileItem)" PublishFolderType="None" />
</ItemGroup>
</Target>

</Project>
12 changes: 12 additions & 0 deletions src/Sentry.Maui/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Reflection;

namespace Sentry.Maui;

internal static class Constants
{
// See: https://github.com/getsentry/sentry-release-registry
public const string SdkName = "sentry.dotnet.maui";

public static string SdkVersion = typeof(SentryMauiOptions).Assembly
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()!.InformationalVersion;
}
15 changes: 15 additions & 0 deletions src/Sentry.Maui/MauiEventProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Sentry.Extensibility;

namespace Sentry.Maui;

internal class MauiEventProcessor : ISentryEventProcessor
{
public SentryEvent Process(SentryEvent @event)
{
// Set SDK name and version for MAUI
@event.Sdk.Name = Constants.SdkName;
@event.Sdk.Version = Constants.SdkVersion;

return @event;
}
}
6 changes: 6 additions & 0 deletions src/Sentry.Maui/SentryMauiInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ internal class SentryMauiInitializer : IMauiInitializeService
public void Initialize(IServiceProvider services)
{
var options = services.GetRequiredService<IOptions<SentryMauiOptions>>().Value;

#if ANDROID
var context = global::Android.App.Application.Context;
SentrySdk.Init(context, options);
#else
SentrySdk.Init(options);
#endif
}
}
6 changes: 5 additions & 1 deletion src/Sentry.Maui/SentryMauiOptionsSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public override void Configure(SentryMauiOptions options)
// We'll initialize the SDK in SentryMauiInitializer
options.InitializeSdk = false;

// TODO: Anything MAUI specific for setting up the options. (Can inject dependencies.)
// Global Mode makes sense for client apps
options.IsGlobalModeEnabled = true;

// We'll use an event processor to set things like SDK name
options.AddEventProcessor(new MauiEventProcessor());
}
}
10 changes: 8 additions & 2 deletions src/Sentry/Android/SentrySdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ public static IDisposable Init(
Action<SentryOptions>? configureOptions)
{
var options = new SentryOptions();
configureOptions?.Invoke(options);
return Init(context, options);
}

public static IDisposable Init(
global::Android.Content.Context context,
SentryOptions options)
{
// TODO: Pause/Resume
options.AutoSessionTracking = true;
options.IsGlobalModeEnabled = true;
Expand All @@ -27,8 +35,6 @@ public static IDisposable Init(
return evt;
}));

configureOptions?.Invoke(options);

Sentry.Android.SentryAndroid.Init(context, new JavaLogger(options),
new ConfigureOption(o =>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[assembly: System.Runtime.Versioning.TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName="")]
namespace Microsoft.Maui.Hosting
{
public static class SentryMauiAppBuilderExtensions { }
}
namespace Sentry.Maui
{
public class SentryMauiOptions : Sentry.Extensions.Logging.SentryLoggingOptions
{
public SentryMauiOptions() { }
}
}
13 changes: 13 additions & 0 deletions test/Sentry.Maui.Tests/ApiApprovalTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Sentry.Tests;

namespace Sentry.Maui.Tests;

[UsesVerify]
public class ApiApprovalTests
{
[Fact]
public Task Run()
{
return typeof(SentryMauiOptions).Assembly.CheckApproval();
}
}
46 changes: 46 additions & 0 deletions test/Sentry.Maui.Tests/SentryMauiAppBuilderExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,50 @@ public void CanUseSentry_WithConfigurationAndOptions()
Assert.Equal(DsnSamples.ValidDsnWithoutSecret, options.Dsn);
Assert.True(options.Debug);
}

[Fact]
public void UseSentry_EnablesGlobalMode()
{
// Arrange
var builder = MauiApp.CreateBuilder();

// Act
_ = builder.UseSentry(DsnSamples.ValidDsnWithoutSecret);

using var app = builder.Build();
var options = app.Services.GetRequiredService<IOptions<SentryMauiOptions>>().Value;

// Assert
Assert.True(options.IsGlobalModeEnabled);
}

[Fact]
public void UseSentry_SetsMauiSdkNameAndVersion()
{
// Arrange
SentryEvent @event = null;
var builder = MauiApp.CreateBuilder()
.UseSentry(options =>
{
options.Dsn = DsnSamples.ValidDsnWithoutSecret;
options.BeforeSend = e =>
{
// capture the event
@event = e;

// but don't actually send it
return null;
};
});

// Act
using var app = builder.Build();
var client = app.Services.GetRequiredService<ISentryClient>();
client.CaptureMessage("test");

// Assert
Assert.NotNull(@event);
Assert.Equal(Constants.SdkName, @event.Sdk.Name);
Assert.Equal(Constants.SdkVersion, @event.Sdk.Version);
}
}