Integrating with Blazor Server application
Use Rollbar from within a Blazor Server
Working Sample
For the working sample, please look here.
Recommended Rollbar Modules/Packages
Whenever you are trying to integrate Rollbar.NET SDK into an existing Blazor Server based application, the most optimal way is to do it via our Rollbar.NetCore.AspNet module. It will also bring in the required dependencies that are Rollbar core module/package and Rollbar.NetPlatformExtensions module/package:
dotnet add package Rollbar.NetCore.AspNet
Integrating Rollbar Within Asp.Net Core Built-In Logging Infrastructure and Using the Rollbar Middleware
Modify Startup.cs
file like so (all 4 steps):
//...
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Rollbar;
using Rollbar.NetCore.AspNet;
//...
public class Startup
{
//...
public void ConfigureServices(IServiceCollection services)
{
// STEP.1 - enable Http Context Accessor:
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
// STEP.2 - Setup Rollbar Infrastructure:
ConfigureRollbarInfrastructure();
// STEP.3 - Add Rollbar logger with properly configured log level filter:
services.AddRollbarLogger(loggerOptions =>
{
loggerOptions.Filter =
(loggerName, loglevel) => loglevel >= LogLevel.Trace;
});
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddSingleton<WeatherForecastService>();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
// STEP.4 - Use Rollbar middleware:
app.UseRollbarMiddleware();
// Any other middleware component intended to be "monitored" by Rollbar middleware
// go below this line:
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
}
// STEP.2 - Setup Rollbar Infrastructure:
private void ConfigureRollbarInfrastructure()
{
RollbarInfrastructureConfig config = new RollbarInfrastructureConfig(
RollbarSamplesSettings.AccessToken,
RollbarSamplesSettings.Environment
);
RollbarDataSecurityOptions dataSecurityOptions = new RollbarDataSecurityOptions();
dataSecurityOptions.ScrubFields = new string[]
{
"url",
"method",
};
config.RollbarLoggerConfig.RollbarDataSecurityOptions.Reconfigure(dataSecurityOptions);
RollbarInfrastructure.Instance.Init(config);
// Optionally:
RollbarInfrastructure.Instance.QueueController.InternalEvent += OnRollbarInternalEvent;
}
// STEP.2 - Setup Rollbar Infrastructure:
private static void OnRollbarInternalEvent(object sender, RollbarEventArgs e)
{
Console.WriteLine(e.TraceAsString());
RollbarApiErrorEventArgs apiErrorEvent = e as RollbarApiErrorEventArgs;
if(apiErrorEvent != null)
{
//TODO: handle/report Rollbar API communication error event...
return;
}
CommunicationEventArgs commEvent = e as CommunicationEventArgs;
if(commEvent != null)
{
//TODO: handle/report Rollbar API communication event...
return;
}
CommunicationErrorEventArgs commErrorEvent = e as CommunicationErrorEventArgs;
if(commErrorEvent != null)
{
//TODO: handle/report basic communication error while attempting to reach Rollbar API service...
return;
}
InternalErrorEventArgs internalErrorEvent = e as InternalErrorEventArgs;
if(internalErrorEvent != null)
{
//TODO: handle/report basic internal error while using the Rollbar Notifier...
return;
}
}
}
Updated 11 months ago