Blazor Interactive SSR

Learn about Sentry's .NET integration with Blazor Interactive Server-Side Rendering.

Blazor Interactive SSR (also known as Blazor Server) runs components on the server and streams UI updates to the browser over a SignalR connection (circuit). Sentry integrates with Blazor Interactive SSR through the Sentry.AspNetCore NuGet package, which captures unhandled exceptions and provides optional tracing via OpenTelemetry.

Add the Sentry dependency to your Blazor Server application:

Copied
dotnet add package Sentry.AspNetCore -v 6.6.0

This package extends Sentry.Extensions.Logging. In addition to Blazor Server features, you'll also get the ILogger<T> integration and all features available in the main Sentry SDK.

Initialize Sentry by calling UseSentry on your WebHostBuilder:

Program.cs
Copied
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();

builder.WebHost.UseSentry(options =>
{
    options.Dsn = "___PUBLIC_DSN___";
    // When configuring for the first time, to see what the SDK is doing:
    options.Debug = true;
    // Adds request URL and headers, IP and name for users, etc.
    options.SendDefaultPii = true;
    // ___PRODUCT_OPTION_START___ performance
    options.TracesSampleRate = 0.1;
    // ___PRODUCT_OPTION_END___ performance
});

var app = builder.Build();

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();

app.MapBlazorHub();
app.MapFallbackToPage("/_Host");

app.Run();

Tracing events in Blazor Server applications is challenging due to the socket based nature of communications.

However, on .NET 10 and later, Blazor emits OpenTelemetry activities for circuit lifecycle events, component navigation, and UI interactions that can be used to provide some limited tracing. The Sentry.Samples.AspNetCore.Blazor.Server sample project gives an example of this.

See OpenTelemetry Support for installation and setup instructions.

Wire up Sentry as an OpenTelemetry exporter, subscribe to the Blazor activity sources, and register helper services for circuit and breadcrumb tracking:

Program.cs
Copied
using Microsoft.AspNetCore.Components.Server.Circuits;
using OpenTelemetry.Trace;
using Sentry.OpenTelemetry;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();

builder.Services.AddOpenTelemetry()
    .WithTracing(tracing =>
    {
        tracing.AddSource("Microsoft.AspNetCore.Components");
        tracing.AddSource("Microsoft.AspNetCore.Components.Server.Circuits");
        tracing.AddAspNetCoreInstrumentation();
        // Export traces to Sentry
        tracing.AddSentry();
    });

builder.WebHost.UseSentry(options =>
{
    options.Dsn = "___PUBLIC_DSN___";
    options.UseOpenTelemetry();
    options.TracesSampleRate = 0.1;
    // Rewrites /_blazor SignalR transaction names to the actual Blazor route
    options.AddEventProcessor(new BlazorEventProcessor());
});

// Track circuit lifecycle events and add navigation/UI breadcrumbs
builder.Services.AddSingleton<BlazorSentryIntegration>();
builder.Services.AddHostedService(sp => sp.GetRequiredService<BlazorSentryIntegration>());
builder.Services.AddScoped<CircuitHandler, SentryCircuitHandler>();

var app = builder.Build();

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();

app.MapBlazorHub();
app.MapFallbackToPage("/_Host");

app.Run();

The BlazorSentryIntegration, SentryCircuitHandler, and BlazorEventProcessor services shown above are not part of the Sentry integration. You can copy them from the Blazor Server sample into your application if they prove useful for your use case.

They provide the following features:

ServicePurpose
BlazorSentryIntegrationSubscribes to Blazor activity sources and adds navigation/event breadcrumbs to the active Sentry scope
SentryCircuitHandlerHooks into circuit open/close events to track circuit lifetime and prevent memory leaks
BlazorEventProcessorRewrites uninformative SignalR transaction names (e.g. /_blazor) to the actual Blazor route

This snippet includes an intentional error, so you can test that everything is working as soon as you set it up.

Copied
try
{
    throw null;
}
catch (Exception ex)
{
    SentrySdk.CaptureException(ex);
}

  • Explore practical guides on what to monitor, log, track, and investigate after setup
Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").