Skip to content

Commit

Permalink
Add Blazor WASM detection to set IsGlobalModeEnabled to true (#1931)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Sep 17, 2022
1 parent 83abcc6 commit 4260f15
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 8 additions & 2 deletions samples/Sentry.Samples.AspNetCore.Blazor.Wasm/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net.Http;
using System.Runtime.InteropServices;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Sentry.Samples.AspNetCore.Blazor.Wasm;

Expand All @@ -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
{
Expand All @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions src/Sentry/PlatformAbstractions/RuntimeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ public static bool IsNetCore(this Runtime runtime) =>
/// <returns>True if it's Mono, otherwise false.</returns>
public static bool IsMono(this Runtime runtime) => runtime.StartsWith("Mono");

/// <summary>
/// Is the runtime instance Browser Web Assembly.
/// </summary>
/// <param name="runtime">The runtime instance to check.</param>
/// <returns>True if it's Browser WASM, otherwise false.</returns>
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;
Expand Down
4 changes: 2 additions & 2 deletions src/Sentry/SentryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ public bool IsGlobalModeEnabled
/// <summary>
/// Specifies whether to use global scope management mode.
/// Should be <c>true</c> for client applications and <c>false</c> for server applications.
/// The default is <c>false</c>.
/// The default is <c>false</c>. The default for Blazor WASM, MAUI, and Mobile apps is <c>true</c>.
/// </summary>
public bool IsGlobalModeEnabled { get; set; }
public bool IsGlobalModeEnabled { get; set; } = Runtime.Current.IsBrowserWasm();
#endif

/// <summary>
Expand Down

0 comments on commit 4260f15

Please sign in to comment.