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.
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.
-
- public void ConfigureServices(IServiceCollection services)
- {
-
- services.AddControllers();
- services.AddSwaggerGen(c =>
- {
- c.SwaggerDoc("v1", new OpenApiInfo { Title = "Logs_App_Insights", Version = "v1" });
- });
- services.AddApplicationInsightsTelemetry();
- }
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.
- "ApplicationInsights": {
- "InstrumentationKey": "Copy paste the key from Azure portal (Application Insights)"
- }
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,
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Logging;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
-
- namespace Logs_App_Insights.Controllers
- {
- [ApiController]
- [Route("[controller]")]
- public class WeatherForecastController : ControllerBase
- {
- private static readonly string[] Summaries = new[]
- {
- "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
- };
-
- private readonly ILogger<WeatherForecastController> _logger;
-
- public WeatherForecastController(ILogger<WeatherForecastController> logger)
- {
- _logger = logger;
- }
-
- [HttpGet]
- public IEnumerable<WeatherForecast> Get()
- {
- var iteration = 1;
- _logger.LogDebug($"Debug {iteration}");
- _logger.LogInformation($"Information {iteration}");
- _logger.LogWarning($"Warning {iteration}");
- _logger.LogError($"Error {iteration}");
- _logger.LogCritical($"Critical {iteration}");
- try
- {
- throw new NotImplementedException();
- }
- catch (Exception ex)
- {
- _logger.LogError(ex, ex.Message);
- }
- var rng = new Random();
- return Enumerable.Range(1, 5).Select(index => new WeatherForecast
- {
- Date = DateTime.Now.AddDays(index),
- TemperatureC = rng.Next(-20, 55),
- Summary = Summaries[rng.Next(Summaries.Length)]
- })
- .ToArray();
- }
- }
- }
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,
- {
- "Logging": {
- "ApplicationInsights": {
- "LogLevel": {
- "Default": "Debug",
- "Microsoft": "Error"
- }
- },
- "LogLevel": {
- "Default": "Information",
- "Microsoft": "Warning",
- "Microsoft.Hosting.Lifetime": "Information"
- }
- },
- "AllowedHosts": "*",
- "ApplicationInsights": {
- "InstrumentationKey": "be3c95df-3610-4acb-8787-3d84186de336"
- }
- }
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.
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.