Using Azure Application Insights For Exception Logging In C#

Introduction

 
Azure Application Insights is nothing but the Application Performance Management (APM) service providing by Microsoft Azure. With App insights, you can do monitoring about our applications by using Leverages instrumentation packages It works for apps for a wide variety of platforms including .NET, Java, Javascript, Node.js, Python and so on so forth hosted on-premises or in the Cloud.
 
It provides benefits as,
  • Azure diagnostics.
  • Proactive detection.
  • Exceptions and Performance diagnostics.
  • Interactive data analysis.
  • User and session count.
  • Pageviews and load performance.
  • AJAX calls.
  • Request rates, Dependency rates, response times, and failure rates.

Azure App Insights - Data consumption

 
When you collect data from Azure app insights and later on we can analyze data from the below platforms,
  • Alerts
  • PowerBI
  • Visual Studio
  • API REST
  • Continuous export 
Step 1 - Creating instrumentation key
 
Go to Azure Portal from here.
 
Search for Application Insights in the search box.
 
 
Click on the + New icon button to create a new Application Insights.
 
 
Fill in the following details, 
  • Resource Group - Use the existing one if had already created or create a new resource group with the name NetworkWatcherRG.
  • Name - Provide the name as LogsAppInsights. 
  • Region - Select the desired region.
Using Azure Application Insights for Exception Logging in C#
 
Browse the resource group where you will find the LogsAppInsights created and click on the app insights resource and copy the Instrumentation Key from the top as shown below,
 
  
Step 2 - Configure the App Insights 
 
In this demo we were using ASP.NET Core 5.0 application, we will need the .Net 5.0 SDK and Visual Studio 2019 installed in our machine and also the newly created Instrumentation Key to be configured in this project so Applications Insights can work.
 
After creating the ASP.NET Core application from Visual Studio.
 
Installing the required NuGet package,
 
 
 
Adding the Application Insights service in Startup.cs File.
  1. // This method gets called by the runtime. Use this method to add services to the container.  
  2. public void ConfigureServices(IServiceCollection services)  
  3. {  
  4.   
  5.     services.AddControllers();  
  6.     services.AddSwaggerGen(c =>  
  7.     {  
  8.         c.SwaggerDoc("v1"new OpenApiInfo { Title = "Logs_App_Insights", Version = "v1" });  
  9.     });  
  10.     services.AddApplicationInsightsTelemetry();  
  11. }  
Adding the Instrumentation key which is an identifier for your application insights resource. we can pass the instrumentation key inside the service or leave it blank this will use the configuration provider to obtain the key. Let's define the key in the appsettings.json file.
  1. "ApplicationInsights": {  
  2.    "InstrumentationKey""Copy paste the key from Azure portal (Application Insights)"  
  3.  }  
With the above Instrumentation key, we have configured the Application Insights into our application.
 
Step 3 - Saving the Logs to Application Insights
 
In order to save the logs or exceptions we just have to use the ILogger service that is configured by default in ASP.NET Core applications and this is achieved by injecting the ILogger service into whatever class we want. Let's use the weather controller because it already has the ILogger service injected.
 
Different Levels that Logger accepts
  • LogDebug
  • LogInformation
  • LogWarning
  • LogError
  • LogCritical  
Let's add these different Logs in the GET Endpoint of the Weather controller and after running the application we can see those logs in the Azure Application Insights resource,
  1. using Microsoft.AspNetCore.Mvc;  
  2. using Microsoft.Extensions.Logging;  
  3. using System;  
  4. using System.Collections.Generic;  
  5. using System.Linq;  
  6. using System.Threading.Tasks;  
  7.   
  8. namespace Logs_App_Insights.Controllers  
  9. {  
  10.     [ApiController]  
  11.     [Route("[controller]")]  
  12.     public class WeatherForecastController : ControllerBase  
  13.     {  
  14.         private static readonly string[] Summaries = new[]  
  15.         {  
  16.             "Freezing""Bracing""Chilly""Cool""Mild""Warm""Balmy""Hot""Sweltering""Scorching"  
  17.         };  
  18.   
  19.         private readonly ILogger<WeatherForecastController> _logger; // Adding the ILogger service  
  20.   
  21.         public WeatherForecastController(ILogger<WeatherForecastController> logger) // Injecting the Service  
  22.         {  
  23.             _logger = logger;  
  24.         }  
  25.   
  26.         [HttpGet]  
  27.         public IEnumerable<WeatherForecast> Get()  
  28.         {  
  29.             var iteration = 1;  
  30.             _logger.LogDebug($"Debug {iteration}");  
  31.             _logger.LogInformation($"Information {iteration}");  
  32.             _logger.LogWarning($"Warning {iteration}");  
  33.             _logger.LogError($"Error {iteration}");  
  34.             _logger.LogCritical($"Critical {iteration}");  
  35.             try  
  36.             {  
  37.                 throw new NotImplementedException();  
  38.             }  
  39.             catch (Exception ex)  
  40.             {  
  41.                 _logger.LogError(ex, ex.Message);  
  42.             }  
  43.             var rng = new Random();  
  44.             return Enumerable.Range(1, 5).Select(index => new WeatherForecast  
  45.             {  
  46.                 Date = DateTime.Now.AddDays(index),  
  47.                 TemperatureC = rng.Next(-20, 55),  
  48.                 Summary = Summaries[rng.Next(Summaries.Length)]  
  49.             })  
  50.             .ToArray();  
  51.         }  
  52.     }  
  53. }  
Logs in Azure  App Insights
 
Click the LogsAppInsights and in the menu bar, you can find the Logs option under the Monitoring section, click on the Logs option it will take us to NewQuery where we can see all the options.
 
 
On the right-hand side under the Application insights, we can see plenty of options to see the logs with different types.
 
traces 
 
Double click on the traces (is an object in which our log messages are saved) option where it will open in the Query window, click on the Run button to see the output as shown in the below picture,
 
 
As I mentioned above here we can see the Warning, Error, Critical messages identified by traces and also we can write the queries to filter the data by its types and we have an option to store those queries for further reference.
 
Query - Application Insights 
 
Here we are filtering the traces by "timestamp" descending order and with "Critical" message see the image below,
 
 
Also if you see the above image there are plenty of options provided by Azure to save & share the Query and to export the same in whatever format we want.
 

Correlation with HTTP requests

 
One most useful field in that traces is operation_ParentId which allows you to have the correlation id that identifies all the messages that came from the same HTTP request on our Web API,
 
 
 
We can see the same parent id's for all the traces under that HTTP request - API,
 
 
Exceptions
 
We can see the debug and Information exceptions under the exception section where we have to write the query to get that result.
 
We also have an option to see these exceptions in the Failure sections under the Application Insights. See the below image,
 
 
 
A point to mention here by default .Net core gives us the Log options like warning, error, and Critical we need to add a piece of code if we want the Debug and Information logs to be tracked from our project.
 

Setup the Configuration - Debug & Information.

 
Add the below-highlighted code in the appsettings.json file to trace those remaining incidents in Azure App insights,
  1. {  
  2.   "Logging": {  
  3.     "ApplicationInsights": {  
  4.       "LogLevel": {  
  5.         "Default""Debug",  
  6.         "Microsoft""Error"  
  7.       }  
  8.     },  
  9.       "LogLevel": {  
  10.         "Default""Information",  
  11.         "Microsoft""Warning",  
  12.         "Microsoft.Hosting.Lifetime""Information"  
  13.       }  
  14.     },  
  15.   "AllowedHosts""*",  
  16.   "ApplicationInsights": {  
  17.     "InstrumentationKey""be3c95df-3610-4acb-8787-3d84186de336"  
  18.   }  
  19. }  
After running the application we can see the remaining Debug and Information logs in the traces section,
 
 
If you want to track Failed requests, Server response time, server requests, and Availability. Click on the Overview option at the top of the App insights where you can see the Graphical representation of all those information.
 
 
Source code - GitHub
 

Conclusion 

 
Hope this article gives you a clear idea about how to configure the logs with Azure Application Insights and also viewing the logs from the Azure portal with Queries and so on so forth. Thank you for reading, please let me know your questions, thoughts, or feedback in the comments section. I appreciate your feedback and encouragement.
 
Keep learning....!


Similar Articles