Recently I was planning to add logging service in my new side project test-api.net (which provides mock API for test purpose) and found elmah.io interesting so I wanted to give it a try. Maybe this blog post could be a series, if I decide to integrate this service in my project. In this post I will share my first experience with elmah.io from signup to pushing some log messages.
I am adding elmah.io to an ASP.NET Core application and it is currently in beta for Core. ASP.NET Core provides built-in logging out of the box. In addition to that, you can configure logging to use other providers. Add the appropriate package in the project.json file, and then configure it just like any other provider.
Now back to elmah.io. First I checked its features and saw lots of awesome features which will be enough for my requirements. Then I checked its docs and saw that service is well documented and also found that support is fast. After signup you need to create a new organization if you are not part of existing organization. Once you create a new organization, a new log will be created automatically with 1 error message inside. If you are wondering how that error came up in fresh signup, then it’s a sample error log message which you can ignore or delete. Clicking on the log box, you will see charts, numbers, error details etc. You can play with that sample error to know what level of information you can get and what you can do with those log messages.
Now let’s start logging.
- First create a log bucket to hold logs. Click on dashboard to create and see your available log buckets (I prefer to say that). There you can create log bucket to store logs as per its type. For example, General Log (Can use to store general log information like application execution), X-Controller log (use to store error, information related to X controller), Application Exception (for uncaught application errors) etc.
- Now add elmah.io NuGet package in project.json file. This is latest version at this time.
- Configure elmah.io logger middleware in startup.cs file.
app.UseElmahIo("API_KEY", new Guid("LogBucket_Id"));
Here I am adding elmah.io middleware in addition of default console logger. Get your API_KEY (from Profile/API) and LogBucket_Id (log id in which bucket you want to store those logs). Now elmah.io is ready to push any uncaught exception to the cloud. You can configure ElmahIoSettings in middleware too (I will write about this on future post).
- It would be easy if I can view API Key in my dashboard. I spend a minute or two searching API key for first time.
Now let’s test it. Add a line of code to throw generic exception in constructor of any Controller.
throw new Exception("Test exception");
I put this line in one of the constructors of ErrorController and trying to call error page, it will throw an exception (its unhandled) and elmah.io immediately logged that error. Here is my dashboard after I got few logs.
If you want to know more about these exceptions, click one of the errors and it will show you a bunch of properties of that error.
Need more information? Click hamburger menu in top right bar you will see a few information tabs like below:
You can check cookies, server variables of that HTTP Request. I like that fix option it provides. At least you don’t need to type to search that exception type. Click one of that fix option to get quick help.
These settings only log uncaught/unhandled exceptions, but if you want to log any exception manually (like handled exception) then you need to add a bit more code. If you don’t need Context details of error/exception then you can look at elmah.io’s integration for Microsoft.Extensions.Logging. If Context is important of exceptions then you can use ship method. It is an extension method which ship/push an exception with context in your targeted log bucket.
public IActionResult Contact(ContactMessageViewModel model)
throw new Exception("Test exception in contact page.");
//Code to validate model and pushing it to database
//With success message
TempData["MessageSuccess"] = "Message successfully sent.";
return RedirectToAction("Contact", "Home");
catch (Exception ex)
//Pushing exception to elmah
ex.Ship("API_KEY", new Guid("LogBucket_Id"), HttpContext);
TempData["MessageError"] = "Unable to send message. Please try again.";
API_KEY, LogBucket_Id and HttpContext are required parameter for this extension method. Here i am throwing exception, catching it on catch box and logging it manually.
[ProTip] Manage your API_Key and LogBucket_Id in appsettings.json or in any other secure place.
Now let’s test this action. In my case an exception will occur when I try to send a message from the contact form and it will be logged in elmah.io. Go to the elmah.io dashboard and targeted log bucket for that exception you will see error details. If you dig into details of the exception, you will find lots of information. Here I put that exception in a POST method which handles form and I got that form information too.
Lots of information which helps me to debug and resolve any exception/issue faster than figuring it out manually.
That’s it. We successfully log errors with elmah.io from an ASP.NET Core application. In the next post, I will show how to log information, warnings etc and other feature and services of elmah.io.
[Bonus] You can install elmah.io extension in visual studio to view log messages inside your visual studio. Download it from here.
Give elmah.io a try. No credit card required.
Happy Logging. 🙂