diff --git a/CHANGELOG.md b/CHANGELOG.md index 3eb5a38d76..68c91e28cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ - Dont send transaction for OPTIONS web request ([#1921](https://github.com/getsentry/sentry-dotnet/pull/1921)) - Fix missing details when aggregate exception is filtered out ([#1922](https://github.com/getsentry/sentry-dotnet/pull/1922)) - Exception filters should consider child exceptions of an `AggregateException` ([#1924](https://github.com/getsentry/sentry-dotnet/pull/1924)) +- Add Blazor WASM detection to set IsGlobalModeEnabled to true ([#1931](https://github.com/getsentry/sentry-dotnet/pull/1931)) ## 3.21.0 diff --git a/samples/Sentry.Samples.AspNetCore.Blazor.Wasm/Program.cs b/samples/Sentry.Samples.AspNetCore.Blazor.Wasm/Program.cs index cf894608b6..c476d7d6a9 100644 --- a/samples/Sentry.Samples.AspNetCore.Blazor.Wasm/Program.cs +++ b/samples/Sentry.Samples.AspNetCore.Blazor.Wasm/Program.cs @@ -1,4 +1,5 @@ using System.Net.Http; +using System.Runtime.InteropServices; using Microsoft.AspNetCore.Components.WebAssembly.Hosting; using Sentry.Samples.AspNetCore.Blazor.Wasm; @@ -7,6 +8,8 @@ { o.Dsn = "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537"; o.Debug = true; + //IsGlobalModeEnabled will be true for Blazor WASM + Debug.Assert(o.IsGlobalModeEnabled); }); try { @@ -16,8 +19,11 @@ // Captures logError and higher as events builder.Logging.AddSentry(o => o.InitializeSdk = false); - builder.Services.AddScoped(_ => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }); - + builder.Services.AddScoped(_ => + new HttpClient + { + BaseAddress = new(builder.HostEnvironment.BaseAddress) + }); await builder.Build().RunAsync(); } catch (Exception e) diff --git a/src/Sentry/PlatformAbstractions/RuntimeExtensions.cs b/src/Sentry/PlatformAbstractions/RuntimeExtensions.cs index d7398a904a..eb1a1e9291 100644 --- a/src/Sentry/PlatformAbstractions/RuntimeExtensions.cs +++ b/src/Sentry/PlatformAbstractions/RuntimeExtensions.cs @@ -32,6 +32,13 @@ public static bool IsNetCore(this Runtime runtime) => /// True if it's Mono, otherwise false. public static bool IsMono(this Runtime runtime) => runtime.StartsWith("Mono"); + /// + /// Is the runtime instance Browser Web Assembly. + /// + /// The runtime instance to check. + /// True if it's Browser WASM, otherwise false. + internal static bool IsBrowserWasm(this Runtime runtime) => runtime.Identifier == "browser-wasm"; + private static bool StartsWith(this Runtime? runtime, string runtimeName) => runtime?.Name?.StartsWith(runtimeName, StringComparison.OrdinalIgnoreCase) == true || runtime?.Raw?.StartsWith(runtimeName, StringComparison.OrdinalIgnoreCase) == true; diff --git a/src/Sentry/SentryOptions.cs b/src/Sentry/SentryOptions.cs index 0ea15f7568..679bdf5c26 100644 --- a/src/Sentry/SentryOptions.cs +++ b/src/Sentry/SentryOptions.cs @@ -59,9 +59,9 @@ public bool IsGlobalModeEnabled /// /// Specifies whether to use global scope management mode. /// Should be true for client applications and false for server applications. - /// The default is false. + /// The default is false. The default for Blazor WASM, MAUI, and Mobile apps is true. /// - public bool IsGlobalModeEnabled { get; set; } + public bool IsGlobalModeEnabled { get; set; } = Runtime.Current.IsBrowserWasm(); #endif ///