Introduction
In the previous article, we created a C# Web API microservice and then deployed the same to Azure. Today, we will look at adding Application Insights Telemetry to this microservice. Application Insights gives us detailed information on the working of our application which is a microservice in this case. It provides updates on requests, responses, failures, and also gives us custom logging via the ILogger interface that is setup in our application.
So, let us begin.
Creating the Application Insights Service in Azure
Open your Azure portal and search for Application Insights. Once you find it, add a new service as below,
Once the service is created and deployed, copy the Instrumentation key from the overview panel. This would be used in the code in order to send our telemetry data to this Application Insights instance.
Updating the C# Web API microservice to add Application Insights telemetry
Update the “Appsettings.json” file as below,
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
},
"ApplicationInsights": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
},
"ApplicationInsights": {
"InstrumentationKey": "<Application Insights Instrumentation key here>"
},
"AllowedHosts": "*"
}
Next, we need to add the required NuGet package to use in our code. Please see below,
Now, that we have the required NuGet package, add the telemetry to our services as below in the Program.cs file,
using Employee.BL;
using Employee.Common.PublicInterfaces;
using Employee.DAL;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddScoped < IDepartmentDao, DepartmentDao > ();
builder.Services.AddScoped < IEmployeeDao, EmployeeDao > ();
builder.Services.AddScoped < IDepartmentBL, DepartmentBL > ();
builder.Services.AddScoped < IEmployeeBL, EmployeeBL > ();
builder.Services.AddControllers();
builder.Services.AddSwaggerGen(c => {
c.SwaggerDoc("v1", new() {
Title = "Employee.WebAPI", Version = "v1"
});
});
builder.Services.AddApplicationInsightsTelemetry();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (builder.Environment.IsDevelopment()) {
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Employee.WebAPI v1"));
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
We will now add some logging via the ILogger interface to our controllers as below,
using Employee.Common;
using Employee.Common.PublicInterfaces;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
namespace Employee.WebAPI.Controllers {
[Route("api/[controller]")]
[ApiController]
public class DepartmentController: ControllerBase {
private readonly ILogger < DepartmentController > _logger;
private readonly IDepartmentBL _departmentBL;
public DepartmentController(ILogger < DepartmentController > logger, IDepartmentBL departmentBL) {
_logger = logger;
_departmentBL = departmentBL;
}
[HttpGet]
public IEnumerable < Department > Get() {
_logger.LogWarning("The Get all departments API was called");
return _departmentBL.GetAllDepartments;
}
}
}
using Employee.Common.PublicInterfaces;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
namespace Employee.WebAPI.Controllers {
[ApiController]
public class EmployeeController: ControllerBase {
private readonly ILogger < EmployeeController > _logger;
private readonly IEmployeeBL _employeeBL;
public EmployeeController(ILogger < EmployeeController > logger, IEmployeeBL employeeBL) {
_logger = logger;
_employeeBL = employeeBL;
}
[HttpGet]
[Route("api/[controller]/departments")]
public IEnumerable < Common.Employee > Get(int departmentId) {
_logger.LogInformation("The Get employees by department API was called");
return _employeeBL.GetEmployeesByDepartment(departmentId);
}
[HttpGet]
[Route("api/[controller]/details")]
public Common.Employee GetEmployee(int employeeId) {
_logger.LogInformation("The Get employee by Id API was called");
return _employeeBL.GetEmployeesById(employeeId);
}
}
}
We now run the application and make a call to the department and employee controllers via Postman. After that, we go to the Application Insights dashboard, and we can see the requests being made:
The specific logging entries can also be found here,
Summary
In this article, we looked at adding Application Insights telemetry to our application. It was a very simple implementation. However, we can fine-tune this to meet our needs. The main advantage of this is that we get lots of information out of the box and we can use this information to improve the overall performance of our application. In addition to this, we can add our own logging as well. Happy coding!