Logging to elmah.io from WPF

elmah.io logging can be easily added to WPF applications. We don't provide a package specific to WPF, but the Elmah.Io.Client package, combined with a bit of code, will achieve just the same.

To start logging to elmah.io, install the Elmah.Io.Client NuGet package:

Install-Package Elmah.Io.Client
dotnet add package Elmah.Io.Client
<PackageReference Include="Elmah.Io.Client" Version="4.*" />
paket add Elmah.Io.Client

Add the following usings to the App.xaml.cs file:

using Elmah.Io.Client;
using Elmah.Io.Client.Models; // 👈 Required for Elmah.Io.Client v3.x and lower only
using System.Diagnostics;
using System.Security.Principal;
using System.Threading.Tasks;

Add the following code:

private IElmahioAPI logger;

public App()
    logger = ElmahioAPI.Create("API_KEY");

    AppDomain.CurrentDomain.UnhandledException += (sender, args) =>
        LogException(args.ExceptionObject as Exception);

    TaskScheduler.UnobservedTaskException += (sender, args) =>

    Dispatcher.UnhandledException += (sender, args) =>
        if (!Debugger.IsAttached)

private void LogException(Exception exception)
    var baseException = exception.GetBaseException();
    logger.Messages.Create("LOG_ID", new CreateMessage
        DateTime = DateTime.UtcNow,
        Detail = exception?.ToString(),
        Type = baseException?.GetType().FullName,
        Title = baseException?.Message ?? "An error occurred",
        Data = exception.ToDataList(),
        Severity = "Error",
        Source = baseException?.Source,
        User = WindowsIdentity.GetCurrent().Name,

Replace API_KEY with your API key (Where is my API key?) and LOG_ID with the id of the log (Where is my log ID?) where you want errors logged.

This article was brought to you by the elmah.io team. elmah.io is the best error management system for .NET web applications. We monitor your website, alert you when errors start happening, and help you fix errors fast.

See how we can help you monitor your website for crashes Monitor your website