Build status NuGet Samples

Logging to elmah.io from WPF

elmah.io logging can be easily added to WPF applications. To start logging to elmah.io, install the Elmah.Io.Wpf NuGet package:

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

Next, initialize elmah.io in the App.xaml.cs file:

public partial class App : Application
{
    public App()
    {
        ElmahIoWpf.Init(new ElmahIoWpfOptions
        {
            ApiKey = "API_KEY",
            LogId = new Guid("LOG_ID")
        });
    }
}

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.

Note

Remember to generate a new API key with messages_write permission only. This makes it easy to revoke the API key if someone starts sending messages to your log with your key.

That's it. All uncaught exceptions are now logged to elmah.io.

Logging exceptions manually

Once initialized using the Init call, exceptions can be logged manually:

ElmahIoWpf.Log(new Exception());

The Elmah.Io.Wpf package automatically records breadcrumbs when clicking buttons and opening/closing windows. To manually include a breadcrumb you can include the following code:

ElmahIoWpf.AddBreadcrumb(new Client.Breadcrumb(DateTime.UtcNow, severity:"Information", action:"Save", message:"Record save"));

severity can be set to Verbose, Debug, Information, Warning, Error, or Fatal. The value of action is a string of your choice. If using one of the following values, the action will get a special icon in the elmah.io UI: click, submit, navigation, request, error, warning, fatal. The message field can be used to describe the breadcrumb in more detail and/or include IDs or similar related to the breadcrumb.

The number of breadcrumbs to store in memory is 10 as a default. If you want to lower or increase this number, set the MaximumBreadcrumbs property during initialization:

ElmahIoWpf.Init(new ElmahIoWpfOptions
{
    // ...
    MaximumBreadcrumbs = 20,
});

Additional options

Setting application name

The application name can be set on all logged messages by setting the Application property on ElmahIoWpfOptions during initialization:

ElmahIoWpf.Init(new ElmahIoWpfOptions
{
    // ...
    Application = "WPF on .NET 6",    
});

Hooks

The ElmahIoWpfOptions class also supports a range of actions to hook into various stages of logging errors. Hooks are registered as actions when installing Elmah.Io.Wpf:

ElmahIoWpf.Init(new ElmahIoWpfOptions
{
    // ...
    OnFilter = msg =>
    {
        return msg.Type.Equals("System.NullReferenceException");
    },
    OnMessage = msg =>
    {
        msg.Version = "42";
    },
    OnError = (msg, ex) =>
    {
        // Log somewhere else
    }
});

The OnFilter action can be used to ignore/filter specific errors. In this example, all errors of type System.NullReferenceException is ignored. The OnMessage action can be used to decorate/enrich all errors with different information. In this example, all errors get a version number of 42. The OnError action can be used to handle if the elmah.io API is down. While this doesn't happen frequently, you might want to log errors elsewhere.

Legacy

Before the Elmah.Io.Wpf package was developed, this was the recommended way of installing elmah.io in WPF.

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="5.*" />
paket add Elmah.Io.Client

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

using Elmah.Io.Client;
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) =>
        LogException(args.Exception);

    Dispatcher.UnhandledException += (sender, args) =>
    {
        if (!Debugger.IsAttached)
            LogException(args.Exception);
    };
}

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