Azure

Including Software Insights Telemetry To Our Microservice In Azure

Introduction

Within the earlier article, we created a C# Net API microservice after which deployed the identical to Azure. Right now, we are going to have a look at including Software Insights Telemetry to this microservice. Software Insights offers us detailed data on the working of our utility which is a microservice on this case. It supplies updates on requests, responses, failures, and likewise offers us customized logging through the ILogger interface that’s setup in our utility.

So, allow us to start.

Creating the Software Insights Service in Azure

Open your Azure portal and seek for Software Insights. As soon as you discover it, add a brand new service as under,

Adding Application Insights Telemetry To Our Microservice In Azure

Adding Application Insights Telemetry To Our Microservice In Azure

Adding Application Insights Telemetry To Our Microservice In Azure

As soon as the service is created and deployed, copy the Instrumentation key from the overview panel. This may be used within the code so as to ship our telemetry knowledge to this Software Insights occasion.

Updating the C# Net API microservice so as to add Software Insights telemetry

Replace the “Appsettings.json” file as under,

{
  "Logging": {
    "LogLevel": {
      "Default": "Data",
      "Microsoft": "Warning",
      "Microsoft.Internet hosting.Lifetime": "Data"
    },
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Data",
        "Microsoft": "Warning",
        "Microsoft.Internet hosting.Lifetime": "Data"
      }
    }
  },
  "ApplicationInsights": {
    "InstrumentationKey": "<Software Insights Instrumentation key right here>"
  },
  "AllowedHosts": "*"
}

Subsequent, we have to add the required NuGet package deal to make use of in our code. Please see under,

Adding Application Insights Telemetry To Our Microservice In Azure

Adding Application Insights Telemetry To Our Microservice In Azure

Adding Application Insights Telemetry To Our Microservice In Azure

Adding Application Insights Telemetry To Our Microservice In Azure

Now, that we’ve the required NuGet package deal, add the telemetry to our companies as under within the Program.cs file,

utilizing Worker.BL;
utilizing Worker.Frequent.PublicInterfaces;
utilizing Worker.DAL;
utilizing Microsoft.AspNetCore.Builder;
utilizing Microsoft.Extensions.DependencyInjection;
utilizing Microsoft.Extensions.Internet hosting;
utilizing Microsoft.Extensions.Logging;
var builder = WebApplication.CreateBuilder(args);
// Add companies to the container.
builder.Providers.AddScoped < IDepartmentDao, DepartmentDao > ();
builder.Providers.AddScoped < IEmployeeDao, EmployeeDao > ();
builder.Providers.AddScoped < IDepartmentBL, DepartmentBL > ();
builder.Providers.AddScoped < IEmployeeBL, EmployeeBL > ();
builder.Providers.AddControllers();
builder.Providers.AddSwaggerGen(c => {
    c.SwaggerDoc("v1", new() {
        Title = "Worker.WebAPI", Model = "v1"
    });
});
builder.Providers.AddApplicationInsightsTelemetry();
var app = builder.Construct();
// Configure the HTTP request pipeline.
if (builder.Surroundings.IsDevelopment()) {
    app.UseDeveloperExceptionPage();
    app.UseSwagger();
    app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Worker.WebAPI v1"));
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

We are going to now add some logging through the ILogger interface to our controllers as under,

utilizing Worker.Frequent;
utilizing Worker.Frequent.PublicInterfaces;
utilizing Microsoft.AspNetCore.Mvc;
utilizing Microsoft.Extensions.Logging;
utilizing System.Collections.Generic;
namespace Worker.WebAPI.Controllers {
    [Route("api/[controller]")]
    [ApiController]
    public class DepartmentController: ControllerBase {
        personal readonly ILogger < DepartmentController > _logger;
        personal readonly IDepartmentBL _departmentBL;
        public DepartmentController(ILogger < DepartmentController > logger, IDepartmentBL departmentBL) {
                _logger = logger;
                _departmentBL = departmentBL;
            }
            [HttpGet]
        public IEnumerable < Division > Get() {
            _logger.LogWarning("The Get all departments API was referred to as");
            return _departmentBL.GetAllDepartments;
        }
    }
}
utilizing Worker.Frequent.PublicInterfaces;
utilizing Microsoft.AspNetCore.Mvc;
utilizing Microsoft.Extensions.Logging;
utilizing System.Collections.Generic;
namespace Worker.WebAPI.Controllers {
    [ApiController]
    public class EmployeeController: ControllerBase {
        personal readonly ILogger < EmployeeController > _logger;
        personal readonly IEmployeeBL _employeeBL;
        public EmployeeController(ILogger < EmployeeController > logger, IEmployeeBL employeeBL) {
                _logger = logger;
                _employeeBL = employeeBL;
            }
            [HttpGet]
            [Route("api/[controller]/departments")]
        public IEnumerable < Frequent.Worker > Get(int departmentId) {
                _logger.LogInformation("The Get workers by division API was referred to as");
                return _employeeBL.GetEmployeesByDepartment(departmentId);
            }
            [HttpGet]
            [Route("api/[controller]/particulars")]
        public Frequent.Worker GetEmployee(int employeeId) {
            _logger.LogInformation("The Get worker by Id API was referred to as");
            return _employeeBL.GetEmployeesById(employeeId);
        }
    }
}

We now run the applying and make a name to the division and worker controllers through Postman. After that, we go to the Software Insights dashboard, and we will see the requests being made:

Adding Application Insights Telemetry To Our Microservice In Azure

Adding Application Insights Telemetry To Our Microservice In Azure

The precise logging entries may also be discovered right here,

Adding Application Insights Telemetry To Our Microservice In Azure

Abstract

On this article, we checked out including Software Insights telemetry to our utility. It was a quite simple implementation. Nonetheless, we will fine-tune this to satisfy our wants. The primary benefit of that is that we get a lot of data out of the field and we will use this data to enhance the general efficiency of our utility. Along with this, we will add our personal logging as nicely. Comfortable coding!

Tags
Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Close