Logging to elmah.io from Nancy

Nancy is no longer maintained. While the installation steps on this page (probably) still work, we no longer test the Nancy.Elmah package.

As with MVC and WebAPI, Nancy already provides ELMAH support out of the box. Start by installing the Elmah.Io NuGet package:

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

During the installation, you will be asked for your API key (Where is my API key?) and log ID (Where is my log ID?).

To integrate Nancy and ELMAH, Christian Westman already did a great job with his Nancy.Elmah package. Install it using NuGet:

Install-Package Nancy.Elmah
dotnet add package Nancy.Elmah
<PackageReference Include="Nancy.Elmah" Version="0.*" />
paket add Nancy.Elmah

You must install the Elmah.Io package before Nancy.Elmah, because both packages like to add the ELMAH configuration to the web.config file. If you install it the other way around, you will need to add the elmah.io ErrorLog element manually.

For Nancy to know how to log errors to Elmah, you need to add an override of the DefaultNancyBootstrapper. Create a new class in the root named Bootstrapper:

using Nancy;
using Nancy.Bootstrapper;
using Nancy.Elmah;
using Nancy.TinyIoc;

namespace Elmah.Io.NancyExample
    public class Bootstrapper : DefaultNancyBootstrapper
        protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
            base.ApplicationStartup(container, pipelines);
            Elmahlogging.Enable(pipelines, "elmah");

The important thing in the code sample is line 13, where we tell Nancy.Elmah to hook into the pipeline of Nancy for it to catch and log HTTP errors. The second parameter for the Enable-method, lets us define a URL for the ELMAH error page, which can be used as an alternative to elmah.io for quick viewing of errors.

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