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 -IncludePrerelease
dotnet add package Elmah.Io.Wpf --prerelease
<PackageReference Include="Elmah.Io.Wpf" Version="4.0.4-pre" />
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.

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.

Legacy

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