Migrating to the options pattern in the client for ASP.NET Core

We just released a new version of our integration for ASP.NET Core. The new package follow the pattern around configuring other pieces of middleware more closely. The old method is still available, but marked as obsolete. By switching to the new API, you will get an improved configuration flow as well as the option to configure elmah.io in appsettings.json.

Let's look at how you would configure elmah.io i ASP.NET Core before the recent change:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory fac)
{
    app.UseElmahIo("API_KEY", new Guid("LOG_ID"));
}

The API key and log ID were sent directly to the elmah.io middleware. The problem with this approach were, that it didn't follow the configuration flow in similar middleware. Another problem were the lack of ways to register elmah.io for dependency injection. This caused the Ship extension method to look like this:

try
{
    var i = 0;
    var result = 42/i;
}
catch (DivideByZeroException e)
{
    e.Ship("API_KEY", new Guid("LOG_ID"), HttpContext);
}

In case of the System.DivideByZeroException (or any other exception) being thrown, we'd need to repeat the API key and log ID every time we want to log a new exception. Not very nice at all, precious!

With the recent version (3.5.54 when writing this post), we've switched to using the options pattern in ASP.NET Core. Configuring API key and log ID are now done through a new AddElmahIo-method:

public void ConfigureServices(IServiceCollection services)
{
    services.AddElmahIo(o =>
    {
        o.ApiKey = "API_KEY";
        o.LogId = new Guid("LOG_ID");
    });
}

Notice that this is called in the ConfigureServices-method. The AddElmahIo-method configures elmah.io and adds the configuration to DI.

Enabling the middleware is now easy as this:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory fac)
{
    app.UseElmahIo();
}

That's it! With the new approach, shipping exceptions manually is now simpler too:

try
{
    var i = 0;
    var result = 42/i;
}
catch (DivideByZeroException e)
{
    e.Ship(HttpContext);
}

As promised, elmah.io can now be configured through appsettings.json:

{
  "ElmahIo": {
    "ApiKey": "API_KEY",
    "LogId": "LOG_ID"
  }
}

Instead of calling the AddElmahIo-method, you simply use Configure:

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<ElmahIoOptions>(Configuration.GetSection("ElmahIo"));
}

The Logging to elmah.io from ASP.NET Core document is fully updated to reflect the new syntax. Let us know if you experience any problems when upgrading.

elmah.io: Error logging and Uptime Monitoring for your web apps

This blog post is brought to you by elmah.io. elmah.io is error logging, uptime monitoring, deployment tracking, and service heartbeats for your .NET and JavaScript applications. Stop relying on your users to notify you when something is wrong or dig through hundreds of megabytes of log files spread across servers. With elmah.io, we store all of your log messages, notify you through popular channels like email, Slack, and Microsoft Teams, and help you fix errors fast.

See how we can help you monitor your website for crashes Monitor your website