Skip to content

Commit dc782d7

Browse files
Continue with adding MAUI support (#1670)
* Initialize the Android native SDK on MAUI * Add constant to prevent VS breaking for debugger * Add workaround for PublishFolderType warnings * Set global mode for MAUI apps * Set SDK name and version for MAUI * Add API Approval Tests * Update CHANGELOG.md
1 parent 0d4a4dd commit dc782d7

10 files changed

+132
-4
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
### Features
66

7-
- Initial support for .NET MAUI ([#1663](https://github.com/getsentry/sentry-dotnet/pull/1663))
7+
- Initial support for .NET MAUI ([#1663](https://github.com/getsentry/sentry-dotnet/pull/1663)) ([#1670](https://github.com/getsentry/sentry-dotnet/pull/1670))
88
- 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))
99

1010
### Fixes

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

+14
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@
2929
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
3030
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>
3131
<TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
32+
33+
<!--
34+
This avoids attaching a debugger when we crash the app when targeting Windows while running in Visual Studio.
35+
It is not strictly necessary, but makes the demo sligtly cleaner.
36+
-->
37+
<DefineConstants>DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION</DefineConstants>
38+
3239
</PropertyGroup>
3340

3441
<ItemGroup>
@@ -53,4 +60,11 @@
5360
<ProjectReference Include="..\..\src\Sentry.Maui\Sentry.Maui.csproj" />
5461
</ItemGroup>
5562

63+
<!-- Workaround for https://github.com/dotnet/maui/issues/7272 -->
64+
<Target Name="_SetPublishFolderTypeNoneOnDocFileItems" BeforeTargets="_ComputePublishLocation">
65+
<ItemGroup>
66+
<ResolvedFileToPublish Update="@(DocFileItem)" PublishFolderType="None" />
67+
</ItemGroup>
68+
</Target>
69+
5670
</Project>

src/Sentry.Maui/Constants.cs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.Reflection;
2+
3+
namespace Sentry.Maui;
4+
5+
internal static class Constants
6+
{
7+
// See: https://github.com/getsentry/sentry-release-registry
8+
public const string SdkName = "sentry.dotnet.maui";
9+
10+
public static string SdkVersion = typeof(SentryMauiOptions).Assembly
11+
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()!.InformationalVersion;
12+
}

src/Sentry.Maui/MauiEventProcessor.cs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Sentry.Extensibility;
2+
3+
namespace Sentry.Maui;
4+
5+
internal class MauiEventProcessor : ISentryEventProcessor
6+
{
7+
public SentryEvent Process(SentryEvent @event)
8+
{
9+
// Set SDK name and version for MAUI
10+
@event.Sdk.Name = Constants.SdkName;
11+
@event.Sdk.Version = Constants.SdkVersion;
12+
13+
return @event;
14+
}
15+
}

src/Sentry.Maui/SentryMauiInitializer.cs

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ internal class SentryMauiInitializer : IMauiInitializeService
99
public void Initialize(IServiceProvider services)
1010
{
1111
var options = services.GetRequiredService<IOptions<SentryMauiOptions>>().Value;
12+
13+
#if ANDROID
14+
var context = global::Android.App.Application.Context;
15+
SentrySdk.Init(context, options);
16+
#else
1217
SentrySdk.Init(options);
18+
#endif
1319
}
1420
}

src/Sentry.Maui/SentryMauiOptionsSetup.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ public override void Configure(SentryMauiOptions options)
1616
// We'll initialize the SDK in SentryMauiInitializer
1717
options.InitializeSdk = false;
1818

19-
// TODO: Anything MAUI specific for setting up the options. (Can inject dependencies.)
19+
// Global Mode makes sense for client apps
20+
options.IsGlobalModeEnabled = true;
21+
22+
// We'll use an event processor to set things like SDK name
23+
options.AddEventProcessor(new MauiEventProcessor());
2024
}
2125
}

src/Sentry/Android/SentrySdk.cs

+8-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ public static IDisposable Init(
1515
Action<SentryOptions>? configureOptions)
1616
{
1717
var options = new SentryOptions();
18+
configureOptions?.Invoke(options);
19+
return Init(context, options);
20+
}
21+
22+
public static IDisposable Init(
23+
global::Android.Content.Context context,
24+
SentryOptions options)
25+
{
1826
// TODO: Pause/Resume
1927
options.AutoSessionTracking = true;
2028
options.IsGlobalModeEnabled = true;
@@ -27,8 +35,6 @@ public static IDisposable Init(
2735
return evt;
2836
}));
2937

30-
configureOptions?.Invoke(options);
31-
3238
Sentry.Android.SentryAndroid.Init(context, new JavaLogger(options),
3339
new ConfigureOption(o =>
3440
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[assembly: System.Runtime.Versioning.TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName="")]
2+
namespace Microsoft.Maui.Hosting
3+
{
4+
public static class SentryMauiAppBuilderExtensions { }
5+
}
6+
namespace Sentry.Maui
7+
{
8+
public class SentryMauiOptions : Sentry.Extensions.Logging.SentryLoggingOptions
9+
{
10+
public SentryMauiOptions() { }
11+
}
12+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Sentry.Tests;
2+
3+
namespace Sentry.Maui.Tests;
4+
5+
[UsesVerify]
6+
public class ApiApprovalTests
7+
{
8+
[Fact]
9+
public Task Run()
10+
{
11+
return typeof(SentryMauiOptions).Assembly.CheckApproval();
12+
}
13+
}

test/Sentry.Maui.Tests/SentryMauiAppBuilderExtensionsTests.cs

+46
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,50 @@ public void CanUseSentry_WithConfigurationAndOptions()
9595
Assert.Equal(DsnSamples.ValidDsnWithoutSecret, options.Dsn);
9696
Assert.True(options.Debug);
9797
}
98+
99+
[Fact]
100+
public void UseSentry_EnablesGlobalMode()
101+
{
102+
// Arrange
103+
var builder = MauiApp.CreateBuilder();
104+
105+
// Act
106+
_ = builder.UseSentry(DsnSamples.ValidDsnWithoutSecret);
107+
108+
using var app = builder.Build();
109+
var options = app.Services.GetRequiredService<IOptions<SentryMauiOptions>>().Value;
110+
111+
// Assert
112+
Assert.True(options.IsGlobalModeEnabled);
113+
}
114+
115+
[Fact]
116+
public void UseSentry_SetsMauiSdkNameAndVersion()
117+
{
118+
// Arrange
119+
SentryEvent @event = null;
120+
var builder = MauiApp.CreateBuilder()
121+
.UseSentry(options =>
122+
{
123+
options.Dsn = DsnSamples.ValidDsnWithoutSecret;
124+
options.BeforeSend = e =>
125+
{
126+
// capture the event
127+
@event = e;
128+
129+
// but don't actually send it
130+
return null;
131+
};
132+
});
133+
134+
// Act
135+
using var app = builder.Build();
136+
var client = app.Services.GetRequiredService<ISentryClient>();
137+
client.CaptureMessage("test");
138+
139+
// Assert
140+
Assert.NotNull(@event);
141+
Assert.Equal(Constants.SdkName, @event.Sdk.Name);
142+
Assert.Equal(Constants.SdkVersion, @event.Sdk.Version);
143+
}
98144
}

0 commit comments

Comments
 (0)