Integrating with WPF 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 WPF-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 App.xaml.cs

namespace Sample
{
    //...
    using Rollbar;
    using Rollbar.DTOs;

    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App 
      : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
          // Setup the Rollbar:
          ConfigureRollbar();
          
          // Let's log this:
          RollbarLocator.RollbarInstance.Info(
            "WpfApp sample: Rollbar.NET is ready to roll..."
          );
          
          // Add the event handler for the application unhandled exceptions:
          base.DispatcherUnhandledException += 
            App_DispatcherUnhandledException;

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

          base.OnStartup(e);
        }
        
        private void App_DispatcherUnhandledException(
          object sender, 
          DispatcherUnhandledExceptionEventArgs e
        )
        {
            ReportAsCriticalToRollbar(e.Exception);
            e.Handled = true;
        }

        private void CurrentDomain_UnhandledException(
          object sender, 
          UnhandledExceptionEventArgs e
        )
        {
            ReportAsCriticalToRollbar(e.ExceptionObject);
        }

                private void ReportAsCriticalToRollbar(object data)
        {
            TimeSpan rollbarDeliveryTimeout = TimeSpan.FromSeconds(3);
            RollbarLocator.RollbarInstance.AsBlockingLogger(rollbarDeliveryTimeout).Critical(data);
        }

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

            // initialize Rollbar infrastructure:
            RollbarInfrastructure
              .Instance
              .Init(rollbarInfrastructureConfig);

            // optionally, monitor this 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;
            }
        }

    }
}

Did this page help you?