Integrating with Windows 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 Windows Forms-based application, the most optimal way is to do it via our Rollbar module. Optionally, you may want to also use other modules/packages of the SDK: Rollbar.App.Config and Rollbar.OfflinePersistence:

dotnet add package Rollbar

// optionally:
dotnet add package Rollbar.App.Config
dotnet add package Rollbar.OfflinePersistence

Modify Program.cs:

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

[STAThread]
static void Main()
{
  // Setup the Rollbar Notifier:
  ConfigureRollbar();

  // Let's log this:
  RollbarLocator.RollbarInstance.Info(
    "WindowsFormsApp sample: Rollbar.NET is ready to roll..."
  );

  // Add the event handler for handling UI thread exceptions to the event:
  Application.ThreadException += Application_ThreadException;

  // Set the unhandled exception mode to force all Windows Forms errors 
  // to go through our handler:
  Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

  // Add the event handler for handling non-UI thread exceptions to the event:
  AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

  
  Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);
  Application.Run(new Form1());
}

private static void CurrentDomain_UnhandledException(
  object sender, 
  UnhandledExceptionEventArgs e
)
{
  RollbarLocator
    .RollbarInstance
    .AsBlockingLogger(TimeSpan.FromSeconds(3))
    .Critical(e.ExceptionObject);
}

private static void Application_ThreadException(
  object sender, 
  System.Threading.ThreadExceptionEventArgs e
)
{
  RollbarLocator
    .RollbarInstance
    .AsBlockingLogger(TimeSpan.FromSeconds(3))
    .Critical(e.Exception);
}

private static void ConfigureRollbar()
{
  // create valid Rollbar configuration:
  RollbarInfrastructureConfig rollbarInfrastructureConfig = 
    new RollbarInfrastructureConfig(
      RollbarSamplesSettings.AccessToken,
      RollbarSamplesSettings.Environment
    );

  // optionally;
  RollbarDataSecurityOptions dataSecurityOptions = 
    new RollbarDataSecurityOptions();
  dataSecurityOptions.ScrubFields = new string[]
  {
    "access_token", // normally, you do not want scrub this specific field (it is operationally critical), but it just proves safety net built into the notifier... 
    "username",
  };
  rollbarInfrastructureConfig
    .RollbarLoggerConfig
    .RollbarDataSecurityOptions
    .Reconfigure(dataSecurityOptions);

  // optionally;
  RollbarPayloadAdditionOptions payloadAdditionOptions = 
    new RollbarPayloadAdditionOptions();
  payloadAdditionOptions.Person = new Person("007") 
  { 
    Email = "[email protected]", 
    UserName = "JBOND" 
    };
  rollbarInfrastructureConfig
    .RollbarLoggerConfig
    .RollbarPayloadAdditionOptions
    .Reconfigure(payloadAdditionOptions);

  RollbarInfrastructure.Instance.Init(rollbarInfrastructureConfig);

  // optionally, monitor Rollbar internal events:
  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;
  }
}