Logging to elmah.io from Windows Forms

elmah.io logging can be easily added to Windows Forms applications. We don't provide a package specific for WinForms, 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="5.*" />
paket add Elmah.Io.Client

Add the following usings to the Program.cs file:

using Elmah.Io.Client;
using System.Security.Principal;
using System.Threading;

Add an event handler to the ThreadException event in the Main method:

Application.ThreadException += Application_ThreadException;

Finally, add the Application_ThreadException method:

static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
    var logger = ElmahioAPI.Create("API_KEY");
    var exception = e.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,
    });

    Application.Exit();
}

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 example closes the application when an error occurs. If you only want to log the error, make sure to re-use the logger object:

private static IElmahioAPI logger;

static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
    if (logger == null)
    {
        logger = ElmahioAPI.Create("API_KEY");
    }

    // ...
}


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