Build status NuGet Samples

Logging to elmah.io from WinUI

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

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

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

public partial class App : Application
{
    public App()
    {
        ElmahIoWinUI.Init(new ElmahIoWinUIOptions(
            "API_KEY",
            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:

ElmahIoWinUI.Log(new Exception());

The Elmah.Io.WinUI package provide an API to manually include breadcrumbs:

ElmahIoWinUI.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:

ElmahIoWinUI.Init(new ElmahIoWinUIOptions(/* ... */)
{
    // ...
    MaximumBreadcrumbs = 20,
});

Additional options

Setting application name

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

ElmahIoWinUI.Init(new ElmahIoWinUIOptions(/* ... */)
{
    // ...
    Application = "WinUI on .NET 8",    
});

Hooks

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

ElmahIoWinUI.Init(new ElmahIoWinUIOptions(/* ... */)
{
    // ...
    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.


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