Integrating with ASP.NET Web Forms based Applications

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 WebForms application, the most optimal way is to do it via our Rollbar.Net.AspNet.Mvc module. It will also bring in the required dependencies that are Rollbar core module/package and Rollbar.Net.AspNet module/package:

dotnet add package Rollbar.Net.AspNet.Mvc

Modify Global.asax.cs

//...
using Rollbar;
//...

public class Global : HttpApplication
{
  void Application_Start(object sender, EventArgs e)
  {
    ConfigureRollbar();

    // Code that runs on application startup
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
  }

  /// <summary>
  /// Handles the Error event on the Application level.
  /// </summary>
  /// <param name="sender">The source of the event.</param>
  /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  void Application_Error(object sender, EventArgs e)
  {
    Exception exception = Server.GetLastError();

    // Let's report to Rollbar on the Application/Global Level: 
    var metaData = new Dictionary<string, object>();
    metaData.Add("reportLevel", "GlobalLevel");
    RollbarLocator.RollbarInstance.Error(exception, metaData);

    if (exception is HttpUnhandledException)
    {
      // Pass the error on to the error page. 
      Server.Transfer("ErrorPage.aspx?handler=Application_Error%20-%20Global.asax", true);
    }
  }

  /// <summary>
  /// Handles the Error event on the Page level.
  /// </summary>
  /// <param name="sender">The source of the event.</param>
  /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  private void Page_Error(object sender, EventArgs e)
  {
    // Get last error from the server.
    Exception exception = Server.GetLastError();

    // Let's report to Rollbar on the Page Level:
    var metaData = new Dictionary<string, object>();
    metaData.Add("reportLevel", "PageLevel");
    RollbarLocator.RollbarInstance.Error(exception, metaData);

    // Handle specific exception.
    if(exception is InvalidOperationException)
    {
      // Pass the error on to the error page.
      Server.Transfer(
        "ErrorPage.aspx?handler=Page_Error%20-%20Default.aspx",
        true
      );
    }
  }
  
  /// <summary>
  /// Configures the Rollbar singleton-like notifier.
  /// </summary>
  private void ConfigureRollbar()
  {
    RollbarInfrastructureConfig rollbarInfrastructureConfig = new RollbarInfrastructureConfig(
      RollbarSamplesSettings.AccessToken,
      RollbarSamplesSettings.Environment
    );
    RollbarInfrastructure.Instance.Init(rollbarInfrastructureConfig);

    // optionally, if you would like to monitor Rollbar internal events within your application:
    RollbarInfrastructure.Instance.QueueController.InternalEvent += OnRollbarInternalEvent;
  }

  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;
    }
  }

}