Building a .NET 7 Microservice with Monitoring and Centralized Logging on Gitpod
Introduction
Microservices have become an integral part of modern application architecture, allowing teams to build scalable, maintainable, and independently deployable modules. With the rise of distributed systems, monitoring and centralized logging have become crucial components to ensure the system’s health and track issues. In this tutorial, we’ll walk through creating a .NET microservice using the latest .NET 7 framework on Gitpod, a cloud-based development environment. We’ll focus on adding monitoring capabilities and centralized logging, ensuring our service is robust and maintainable.
Step 1: Install .NET SDK
Before diving into the code, we need to set up our environment. The first step is to install the .NET SDK (Software Development Kit), which provides all the necessary tools to build .NET apps.
Open your Gitpod terminal and run the following commands:
sudo apt-get update
sudo apt-get install -y dotnet-sdk-7.0
To ensure that dotnet tools are accessible from any directory, add them to the path variable:
cat << \EOF >> ~/.bash_profile
# Add .NET Core SDK tools
export PATH="$PATH:/home/gitpod/.dotnet/tools"
EOF
Reload your bash_profile to apply the changes:
source ~/.bash_profile
Step 2: Create a New Web API Project
With our environment ready, it’s time to create our microservice. Run the following command to generate a new Web API project:
dotnet new webapi -n MonitoringMicroservice
cd MonitoringMicroservice
Step 3: Configuring Monitoring
For our microservice, we’ll use a popular monitoring tool called “Prometheus”. It’s an open-source system monitoring and alerting toolkit.
First, add the required package:
dotnet add package prometheus-net.AspNetCore
Now, update your Program.cs
to configure monitoring:
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHealthChecks()
.AddCheck("self", () => HealthCheckResult.Healthy());
var app = builder.Build();
app.UseHealthChecks("/health");
app.UseMetricServer();
app.MapGet("/", (Func<string>)(() => "Hello, World!"));
app.Run();
Step 4: Centralized Logging with Serilog
Serilog is a versatile logging library for .NET, and it’s perfect for our use case.
Install the required packages:
dotnet add package Serilog
dotnet add package Serilog.Sinks.Console
dotnet add package Serilog.AspNetCore
Update Program.cs
:
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Serilog;
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.CreateLogger();
var builder = WebApplication.CreateBuilder(args);
builder.Host.UseSerilog();
builder.Services.AddHealthChecks()
.AddCheck("self", () => HealthCheckResult.Healthy());
var app = builder.Build();
app.UseHealthChecks("/health");
app.UseMetricServer();
app.MapGet("/", (Func<string>)(() => "Hello, World!"));
app.Run();
Step 5: A Powerful Use Case: User Activity Tracking
Imagine a scenario where you want to track user activities in your system, like logging in, accessing resources, or making updates. With our microservice structure, we can easily integrate such features and monitor any anomalies.
In our service, let’s say we have an endpoint /trackActivity
. Whenever this endpoint is hit, we log the activity and monitor the number of times it has been accessed.
Update Program.cs with this:
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Serilog;
using Prometheus;
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.CreateLogger();
var builder = WebApplication.CreateBuilder(args);
builder.Host.UseSerilog();
builder.Services.AddHealthChecks()
.AddCheck("self", () => HealthCheckResult.Healthy());
var app = builder.Build();
app.UseHealthChecks("/health");
app.UseMetricServer();
app.MapGet("/", (Func<string>)(() => "Hello, World!"));
// The tracking endpoint
app.MapPost("/trackActivity", (string activity) =>
{
Log.Information($"User performed: {activity}");
// Other business logic
return Results.Ok($"Tracked: {activity}");
});
app.Run();
By setting up monitoring and logging as described above, we can keep an eye on how often this endpoint is accessed and instantly spot any suspicious activities through our logs.
1. Build and Run the Microservice
First, you need to build and run your microservice. In your Gitpod terminal, navigate to the project directory and execute:
dotnet build
dotnet run
This will start the microservice, and it should be listening for incoming HTTP requests.
2. Test the Endpoints
Once the microservice is running, you can test the different endpoints:
a. Default Endpoint
Access the default endpoint by opening a browser and navigating to http://localhost:5000/
. You should see the message "Hello, World!".
b. Health Check Endpoint
Navigate to http://localhost:5000/health
. If everything is set up correctly, you should see a status of "Healthy".
c. User Activity Tracking Endpoint
To test the /trackActivity
endpoint, you'll need to make a POST request. This can be done using tools like curl
(from the terminal), Postman
, or any other API testing tool.
Using curl
:
curl -X POST -H "Content-Type: application/json" -d '"SampleActivity"' http://localhost:5000/trackActivity
This should return a message like “Tracked: SampleActivity”, and if you’ve set up logging with Serilog to the console, you should also see a log entry saying “User performed: SampleActivity”.
3. Monitor Logs
If you’ve integrated Serilog to log to the console, you should be able to see logs in the terminal where your microservice is running. Monitor these logs to ensure that activities are being logged and to identify any potential errors or issues.
4. Monitoring with Prometheus
If you’ve set up Prometheus for monitoring, you can navigate to the Prometheus UI and set up metrics to monitor the calls to your /trackActivity
endpoint, among other things. This will provide insights into the usage and health of your microservice.
Conclusion
Building robust and scalable microservices has become a staple of modern software development. By leveraging tools like Gitpod, .NET 7, Prometheus, and Serilog, we’ve set up a strong foundation for creating microservices that not only perform well but are also easy to monitor and maintain.
Embracing a cloud-native approach, especially with platforms like Gitpod, simplifies the development lifecycle and encourages best practices. The techniques and tools introduced in this article provide a blueprint to ensure that your microservices are always in top shape, ready to serve, and easy to diagnose whenever issues arise.
Happy coding!