Site hosting news, tutorials, tips, How Tos and more

Configuring Elmah for use with ASP.NET Core

howto

Years ago, I wrote an article on how to configure Elmah to be used on Winhost.  You may have noticed that there is no official support for ASP.NET Core, however, you can use ElmahCore by barestan to perform the same logging to a database, and this blog provides an example on how to set that up.

First, we’ll start by creating a New Project in Visual Studio:

 

 

 

 

services.AddElmah<SqlErrorLog>(options =>
{
    options.ConnectionString = @"connection_string";
});
app.UseElmah();

That’s it.  If you want to know if it’s working, you can generate a sample exception like this:

app.Run(async (context) =>
{                
    await context.Response.WriteAsync("Hello World!");
    int[] numbers = new int[5];
    await context.Response.WriteAsync(numbers[6].ToString());
});

That will generate an “Index was outside the bounds of the array.” exception which will be logged into the database.  You can query the dbo.ELMAH_Error table to see the results.

 

Visit Winhost to learn more about our .NET Core hosting solutions

Rate this article
   

6 Responses
  • Sachin Joshi Reply

    What is SqlErrorLog ?

  • Rei Tu Reply

    I believe it’s a custom class type.

  • Carl Verret Reply

    Thanks, great post ! you made my day!

  • Nikos Reply

    Very good post, even my ConnectionString is ok, erros do not write into my db table ELMAH_Error. Do you have any idea?

    • Rei Tu Reply

      Did you modify the Startup.cs file?

Have a comment? Make it here!

This site uses Akismet to reduce spam. Learn how your comment data is processed.