In this article, we are going to learn how to log error in ASP.NET Core Using elmah.io.

Error logging is an essential part of application development. It tells us where something is going wrong in your application.

Free Trial for 21 Days

Suppose we do not log errors or monitor the application after deployment. In that case, its effect can be seen in various ways, such as Application Performance degradation, using lots of resources, issues from your clients, and an increase in your overall budget.

In another scenario, if you see many people might log error in text files and does not monitor it and suddenly in someday, they get the message “There is not enough space on the disk” they start running to see how does this much space got fully then finally they get to know lots of logs are generated on the machine.

To Overcome the above scenarios, we can use elmah.io, which does more than logging.

elmah.io is an error management system for .NET web applications.

There are various features of elmah.io, such as

Error Management

elmah.io is our error management system for .NET web applications. Log exceptions and messages from your website, get notifications through popular channels like mail, Slack and Microsoft Teams, and finally help fix bugs fast. You will find that elmah.io is the best error tracking option for . NET.

Deployment Tracking

With Deployment Tracking, you get the perfect overview of your different software releases and how each release performs in terms of logged errors and warnings. Deployment Tracking is the perfect companion to error management with elmah.io and integrates with Octopus Deploy, Azure DevOps and more.

Uptime Monitoring

Uptime Monitoring lets you set up one or more HTTP(s) endpoints to monitor. We automatically ping your endpoints every 5 minutes from up to 5 different locations. Uptime Monitoring integrates with your existing error log and notifications already set up. Discover the benefits of combining error logs with uptime checks.

Heartbeats

With Heartbeats configured on all of your services, elmah.io will automatically keep track of your services and scheduled tasks. When a program doesn’t publish a heartbeat in time, you will receive a notification. Reacting on missing heartbeats should be an essential part of your monitoring process.

Read details all features: – https://elmah.io/features/

Free Trial for 21 Days

You will not be required to pay anything. Just create an account and use it. For a free trial, click on the “Free Trial” button.

Table of Contents

  1. Sign up
  2. Update Profile
  3. Creating Organization
  4. Quick Access to Documentation
  5. Let’s Start Implementing
  6. Project Structure
  7. Installing Elmah.Io.AspNetCore Package from NuGet Package Manager
  8. Installing Elmah.Io.AspNetCore Package from NuGet Package Manager Console
  9. Registering AddElmahIo in ConfigureServices
  10. Throwing Error to Log
  11. Getting Error in Email
  12. Logs User Agent Details
  13. Various filters
  14. Issue Tracking
  15. Logging exceptions manually

Sign up

Quick signup only requires an Email Address.

After Signup, just updates few profile details.

Update Profile

The next step after updating the profile is to create an organization or company.

Creating Organization

After Creating Organization Finally, everything is ready for on-boarding you. It will redirect you to the log setup page, where you will find quick documentation to start implementing.

One good thing I liked about this quick documentation is they have generated Apikey. We just require to copy it to Configure service method in Startup Class. Instead of going at some other menu and manually generating them.

Quick Access to Documentation

Now we have documentation, let’s start implementing it. For the demo, we are going to use Visual Studio 2019.

Let’s Start Implementing

We are going to create a new application, as shown below.

Next, we are going to set Project Name “DemoApp” and location. In the last part, we will choose the .Net Core framework and ASP.NET Core Version 5.0 as the framework for application and few advanced settings, such as configuring HTTPS and enabling docker not to enable docker settings for this project.

Now finally, click on create button to create a project.

Project structure

The project structure generated according to the configuration.

After creating the project next, we are going to install the Elmah.Io.AspNetCore Package from NuGet Package.

Installing Elmah.Io.AspNetCore Package from NuGet Package Manager

In this step, we are going to Install the Elmah.Io.AspNetCore Package.

Installing Elmah.Io.AspNetCore Package from NuGet Package Manager Console

If you want to install from Package Manager Console, then.

Install-Package Elmah.Io.AspNetCore

After installing the package next step, we are going to register AddElmahIo in the ConfigureServices method.

Registering AddElmahIo in ConfigureServices

We can directly copy this part from the documentation of elmah.io along with the API key.

To navigate to the document, click on Tutexchange Log — Log setting as shown below.

After click on the log settings, it will show all web or logging frameworks; click on ASP.NET Core framework.

Added AddElmahIo to ConfigureServices method.

After registering the next step, we will Call UseElmahIo in the Configure-method just before UseStaticFiles and UseMvc (in Startup.cs).

After calling the UseElmahIo method in Configure method next, we will throw an error in the Home controller.

Throwing Error to Log

Now let’s run the application and see does it log an error.

The below error is shown as we run the application.

Now let’s sign in into the elmah.io portal and see is error logged.

Wow, we saw an error bar. That’s cool. Without writing much code, we can log errors.

Let’s click on the Error bar and see the details of Error.

Another cool feature is we get an Error in the Email Address we have used to create an account.

Getting Error in Email

We get Errors in our Emails; it is a good feature most of the time we won’t keep the portal open for monitoring, but we do keep seeing emails. This will help us easy to see errors on the go. We also get a link in the email. After clicking on the link, we are directly shown an error which we got on email.

Logs User Agent Details

Also, get other details such as when the error occurred which browser, operating system, accessing platform was used while the error occurred. This information helped the development team to quickly get onto the error and resolve it.

Various filters

We can see logs with various Views by date, country, domain, client IP, and many more valuable filters.

We also get an Issue Tracking system in it. We can directly mark the error as fixed or open.

Issue Tracking

The benefits of marking errors as fixed are:

  • elmah.io will automatically mark all errors within the same error group as fixed.
  • You will get a much better overview of what to fix.
  • When a fixed error reoccurs, you will get a new notification through email, Slack or whatever notification destination you have configured on elmah.io.

Logging exceptions manually

Elmah logs each exception automatically. In some scenarios, we need to log custom errors manually. In that case, we can use the Ship method.

Ship Method: – Ship an exception to elmah.io manually.

In the below method, we have implemented the Ship Method.

public IActionResult Index()
{
    try
    {
        int[] myNumbers = { 1, 2, 3 };
        var data = myNumbers[10];
    }
    catch (Exception e)
    {
        e.Ship(HttpContext);
    }
    return View();
}

Logged Manually Error in elmah.io

Summary

In this article, we learned about how to log errors in ASP.NET Core Using elmah.io in simple steps. It is a Nice tool that handles the entire error Logging and Monitoring part of an application.

By Saineshwar Bageri

I am Microsoft MVP | C# Corner MVP | Code Project MVP | FULL STACK .NET Developer and working on .Net Web Technology (Asp.net, Asp.net Core,.Net Core, C#, Sqlserver, MVC, Windows, Console Application, javascript, jquery, json, ORM Dapper) and also a freelance developer.