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
   

9 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?

  • Sachin Joshi Reply

    Why Elmah not logging error under the catch block ?

    • Rei Tu Reply

      I’m afraid I wouldn’t be able to tell you unless I could examine the code. There are some simple instructions on how to use ElmahCore on GitHub:

      https://github.com/ElmahCore/ElmahCore

  • Vimal Reply

    Hi,
    I have followed the above steps in order to configure Elmah in my .NET Core Api.
    Locally errors are getting logged in Elmah_Error table but on Server errors are not getting logged in table.

    But the twist is after deploying my API on server if I hit http://servername/apiName/elmah link first and then try to test my API on server, then error starts getting log in the table.

    Can you please help.

Leave a Reply to Pierre Cancel reply

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