Sentry for WPF

Learn more about how Sentry's .NET SDK works with Windows Presentation Foundation (WPF) applications.

Sentry's .NET SDK works with Windows Presentation Foundation applications through the Sentry NuGet package. It works with WPF apps running on .NET Framework 4.6.2, .NET Core 3.0 or higher.

Install the NuGet package to add the Sentry dependency:

dotnet add package Sentry -v 7.0.1

Error Monitoring starts working as soon as you initialize the SDK. Logs sent through SentrySdk.Logger and Application Metrics need no extra configuration. You'll learn how to send them in Send Logs and Send Application Metrics.

This guide also enables Tracing.

In addition to initializing the SDK with SentrySdk.Init, you must configure the WPF specific error handler.

The SDK automatically handles AppDomain.UnhandledException. On WPF, for production apps (when no debugger is attached), WPF catches exception to show the dialog to the user. You can also configure your app to capture those exceptions before the dialog shows up:

The snippet also turns on:

Don't need a feature? Remove its options from the snippet.

using System.Windows;
using System.Windows.Threading;
using Sentry;

public partial class App : Application
{
    public App()
    {
        SentrySdk.Init(options =>
        {
            // Tells which project in Sentry to send events to:
            options.Dsn = "___PUBLIC_DSN___";

            // When configuring for the first time, to see what the SDK is doing:
            options.Debug = true;

            // Adds request URL and headers, IP and name for users, etc.
            options.SendDefaultPii = true;
            // Enables Tracing.
            // Set to 1.0 to capture 100% of spans for tracing.
            // We recommend reducing this value in production.
            options.TracesSampleRate = 1.0;
            // Enable Global Mode since this is a client app
            options.IsGlobalModeEnabled = true;

            //TODO: any other options you need go here
        });
        DispatcherUnhandledException += App_DispatcherUnhandledException;
    }

    void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
    {
        SentrySdk.CaptureException(e.Exception);

        // If you want to avoid the application from crashing:
        e.Handled = true;
    }
}

Use SentrySdk.Logger to send logs at different levels. These logs are sent without an additional option.

See Set Up Logs for all available logging methods and formatting options.

SentrySdk.Logger.LogInfo("Order processed");
SentrySdk.Logger.LogWarning("Rate limit reached for /api/orders");

Metrics are enabled by default in Sentry .NET SDK 6.1.0 and later. Use SentrySdk.Metrics to send counters, gauges, and distributions from your app.

See Set Up Metrics for all available metric types and configuration options.

SentrySdk.Metrics.EmitCounter("button_click", 5,
    new Dictionary<string, object> { ["browser"] = "Firefox" });
SentrySdk.Metrics.EmitGauge("page_load", 15.0, MeasurementUnit.Duration.Millisecond,
    new Dictionary<string, object> { ["page"] = "/home" });
SentrySdk.Metrics.EmitDistribution("api_latency", 187, MeasurementUnit.Duration.Millisecond,
    new Dictionary<string, object> { ["endpoint"] = "/api/orders" });

Let's test your setup and confirm that data reaches your Sentry project.

try
{
    throw null;
}
catch (Exception ex)
{
    SentrySdk.CaptureException(ex);
}

To verify tracing for code without automatic instrumentation, create and finish a test transaction after initializing Sentry:

var transaction = SentrySdk.StartTransaction("quickstart-test", "test");
transaction.Finish();

TracesSampleRate controls sampling; it doesn't create transactions by itself. See Custom Instrumentation to trace work in your application.

If you enabled profiling, run a longer sampled transaction after the profiler has initialized. A short verification transaction might finish before the profiler collects any samples.

Run any log and metric examples you've added after SDK initialization. To verify automatic tracing, exercise an instrumented operation, such as an incoming HTTP request or a supported UI interaction.

Logs and metrics are batched. In an async test method, flush pending data before a short-lived test exits:

await SentrySdk.FlushAsync();

Check Issues for the captured error, Logs for log messages, Metrics for application metrics, and Traces for sampled transactions. Error capture alone doesn't verify the other features.

At this point, you should have integrated Sentry into your WPF application. Here's what to explore next:

Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").