Integrating with ASP.NET Core Application

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 ASP.NET Core Web App 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.AddHttpContextAccessor();

    // 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();
  }

  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.UseAuthorization();

    app.UseEndpoints(endpoints =>
                     {
                       endpoints.MapRazorPages();
                     });
  }
  
  
  // 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());

    switch (e)
    {
      case RollbarApiErrorEventArgs apiErrorEvent:
        //TODO: handle/report Rollbar API communication event...
        return;
      case CommunicationEventArgs commEvent:
        //TODO: handle/report basic communication error while attempting to reach Rollbar API service... 
        return;
      case CommunicationErrorEventArgs commErrorEvent:
        //TODO: handle/report basic internal error while using the Rollbar Notifier... 
        return;
      case InternalErrorEventArgs internalErrorEvent:
        //TODO: handle/report Rollbar API communication error event...
     return;
    }

  }  

}

Did this page help you?