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);
}

Repeating API key and log ID every time you'd need 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.



Features steps
We monitor your websites

We monitor your websites

We monitor your websites for crashes and availability. This helps you get an overview of the quality of your applications and to spot trends in your releases.

We notify you

We notify you

We notify you when errors starts happening using Slack, HipChat, mail or other forms of communication to help you react to errors before your users do.

We help you fix bugs

We help you fix bugs

We help you fix bugs quickly by combining error diagnostic information with innovative quick fixes and answers from Stack Overflow and social media.

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