Monitor The Real Time Exception In ASP.NET Core App Using Rollbar

Introduction

 
Rollbar is used to monitor the real-time exception of our application which can be used for different environments like development, staging, and production. In this article, we are going to see how to integrate the Rollbar with our existing ASP.NET Core application.
 

Content

  •  Configuring Rollbar in ASP.NET Core application.
  •  Monitoring the real-time exception 

Configure the Rollbar

 
Create or open the existing ASP.NET Core application.
 
Make sure your application running on ASP.NET Core 2.0+ because Rollbar V2 will not support an earlier version of .NET Core.
 
Install Rollbar using the below command in Package Manager Console,  
  1. Install-Package Rollbar  
Install Rollbar for ASP.NET Core.
  1. Install-Package Rollbar.NetCore.AspNet  
Now, just go to https://rollbar.com and sign up to explore the trial version of Rollbar.
 
Once you are done with sign in, create a new project and go to Settings-> Project Access Token Page to get a post_server key.
 
Monitor The Real Time Exception In ASP.NET Core App Using Rollbar
 
Now, let’s flip into our application to configure Rollbar.
 
Go to Startup.cs file.
 
Add the below method in Startup.cs Class.
  1. private void ConfigureRollbarSingleton()  
  2.         {  
  3.             const string rollbarAccessToken = "[your post_Server Key]";  
  4.             const string rollbarEnvironment = "dev";    
  5.             RollbarLocator.RollbarInstance  
  6.               // minimally required Rollbar configuration:  
  7.               .Configure(new RollbarConfig(rollbarAccessToken) { Environment = rollbarEnvironment });  
  8.         }  
From the above code provide the post server key for rollbarAccessToken.
 
Add the below code in ConfigureServices method.
  1. services.AddRollbarLogger(loggerOptions =>  
  2.             {  
  3.                 loggerOptions.Filter = (loggerName, loglevel) => loglevel >= LogLevel.Trace;  
  4.             });  
  5.   
  6.             ConfigureRollbarSingleton();  

Add the rollbar middleware in configure method.

  1. app.UseRollbarMiddleware();  

Monitoring the real time exception

 
Let’s run the application. I got an exception because my app is not connected with the database, now this should be logged in the Rollbar dashboard.
 
Now, go to the Rollbar dashboard where you can find the exception logged.
 
Monitor The Real Time Exception In ASP.NET Core App Using Rollbar
 
Monitor The Real Time Exception In ASP.NET Core App Using Rollbar
 
From the above figure, you can notice the stack trace of the exception. 
 
Reference
 

Conclusion

 
We have seen how to configure and track the real-time exception in ASP.NET Core application using Rollbar which is really useful to monitor the application which is in production. Let’s see how to log the handled exception and configure the people information in my next article.