Integrating Sentry to Winform/WPF Project

Tram Ho

Like Google Analytics, Sentry allows you to monitor and observe your projects with data sent to your your provided link. These allows you to view various reports such as your userbase usage, error and more. We will dive into how to integrate this service into the app.

Installation and Initialization

Firstly add the nugget package:

Install-Package Sentry -Version 3.0.6

After package has been installed we can then initialize Sentry preferably in the App.cs class as demonstrated below.

App.cs

Inside the App() method add below code.

//Creates sentry options, pass the DSN then Initialize SentrySDK

Catching Errors

To auto catch errors simply add below method in App.xaml.cs and then call it in App() right after the SentrySDK has been Initialized.

App()
…..

Now whenever an error occurs the App_DispatcherUnhandledException is triggered and the content of the error is sent to the provided DSN.

Capturing Messages

One can also capture a bare message which is then sent to Sentry. Typically messages are not emitted, but they can be useful for some teams.

SentrySdk.CaptureMessage("Something went wrong");

Adding Breadcrumbs

To add a breadcrumb to Sentry log simply pass in the Message, assign a Category and Level.

Now when there is an occurrence you will be able to see the breadcrumbs in the Sentry platform.

Identify Users

Users consist of critical pieces of information like name, email etc that provides a unique identity in Sentry. In other to add this at least one must be present in order for Sentry SDK to capture the user:

You can also add the username and ip_address if needed.

Sentry also allows you to add even more features such as Attachments, Custom Tags and many more. You can refer here to access the documentation for further information. Happy Coding!

Share the news now

Source : Viblo