Flavio.Santos.RequestTracking
1.0.2
dotnet add package Flavio.Santos.RequestTracking --version 1.0.2
NuGet\Install-Package Flavio.Santos.RequestTracking -Version 1.0.2
<PackageReference Include="Flavio.Santos.RequestTracking" Version="1.0.2" />
<PackageVersion Include="Flavio.Santos.RequestTracking" Version="1.0.2" />
<PackageReference Include="Flavio.Santos.RequestTracking" />
paket add Flavio.Santos.RequestTracking --version 1.0.2
#r "nuget: Flavio.Santos.RequestTracking, 1.0.2"
#addin nuget:?package=Flavio.Santos.RequestTracking&version=1.0.2
#tool nuget:?package=Flavio.Santos.RequestTracking&version=1.0.2
FDS.FDS.RequestTracking
🚀 FDS.RequestTracking is a lightweight .NET library for tracking and logging HTTP requests, providing an isolated and configurable context for auditing, debugging, and monitoring application requests.
📦 Installation
You can install this package via NuGet Package Manager:
dotnet add package Flavio.Santos.RequestTracking --version 1.0.0
Or using Package Manager Console:
Install-Package Flavio.Santos.RequestTracking -Version 1.0.0
🚀 Usage
Registering Request Tracking in the Dependency Injection Container
To enable request tracking, add the following extension method in your Startup.cs
or Program.cs
:
using FDS.RequestTracking.Extensions;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRequestTracking();
Applying Request Tracking to Controllers
Once registered, the RequestDataFilter
will automatically log HTTP request details.
[ServiceFilter(typeof(RequestDataFilter))]
[ApiController]
[Route("api/[controller]")]
public class SampleController : ControllerBase
{
[HttpGet("track")]
public IActionResult GetTrackedRequest()
{
return Ok("Request is being tracked.");
}
}
Retrieving Logged Request Data
The tracked request data can be retrieved using RequestDataStorage
:
var requestData = RequestDataStorage.GetData(requestId);
if (requestData != null)
{
Console.WriteLine($"Tracked Request: {requestData.Method} {requestData.Path}");
}
Using Request Tracking in a Service Layer
Here is an example of how you can integrate Request Tracking in a service layer following Clean Architecture principles:
using BaseHours.Application.Dtos;
using BaseHours.Application.Interfaces;
using BaseHours.Domain.Entities;
using BaseHours.Domain.Interfaces;
using FDS.DbLogger.PostgreSQL.Published;
using FDS.NetCore.ApiResponse.Models;
using FDS.NetCore.ApiResponse.Results;
using FDS.NetCore.ApiResponse.Types;
using FDS.RequestTracking.Storage;
using Microsoft.AspNetCore.Http;
public class ClientService : IClientService
{
private readonly IClientRepository _clientRepository;
private readonly IAuditLogService _auditLogService;
private readonly IHttpContextAccessor _httpContextAccessor;
public ClientService(IClientRepository clientRepository, IAuditLogService auditLogService, IHttpContextAccessor httpContextAccessor)
{
_clientRepository = clientRepository;
_auditLogService = auditLogService;
_httpContextAccessor = httpContextAccessor;
}
public async Task<Response<ClientDto>> AddAsync(ClientRequestDto request)
{
try
{
var requestId = _httpContextAccessor.HttpContext?.TraceIdentifier;
if (!string.IsNullOrEmpty(requestId))
{
var requestData = RequestDataStorage.GetData(requestId);
if (requestData is not null)
{
await _auditLogService.LogInfoAsync($"Request Data - {requestData.Method} {requestData.Path}{requestData.QueryParams} - {requestData.Timestamp}");
RequestDataStorage.ClearData(requestId);
}
}
if (await _clientRepository.ExistsByNameAsync(request.Name))
{
string msg = "A client with this name already exists.";
await _auditLogService.LogValidationErrorAsync(msg, request);
return Result.Create<ClientDto>(
actionType: ActionType.VALIDATION_ERROR,
message: msg
);
}
var client = new Client(Guid.NewGuid(), request.Name);
await _clientRepository.AddAsync(client);
var clientDto = new ClientDto { Id = client.Id, Name = client.Name };
return Result.Create(
actionType: ActionType.CREATE,
message: "Client created successfully.",
data: clientDto
);
}
catch (Exception ex)
{
return Result.Create<ClientDto>(
actionType: ActionType.ERROR,
message: $"An unexpected error occurred: {ex.Message}"
);
}
}
}
📌 Output Example
Example Logged Request Data
{
"requestId": "abc123",
"method": "GET",
"path": "/api/sample/track",
"queryParams": "?filter=test",
"timestamp": "2024-03-12T14:30:00Z"
}
🎯 Features
- Lightweight and easy-to-integrate request tracking
- Structured logging for HTTP requests
- Works seamlessly with Clean Architecture and SOLID principles
- Provides an audit log for debugging and monitoring
- Built-in request data storage for easy retrieval
- Fully compatible with .NET 8
📜 License
This project is licensed under the MIT License - see the LICENSE file for details.
🔗 Links
🔙 Back to Main README
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
-
net8.0
- Microsoft.AspNetCore.Mvc.Core (>= 2.3.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Flavio.Santos.RequestTracking:
Package | Downloads |
---|---|
Flavio.Santos.DbLogger.PostgreSQL
Library for structured logging in PostgreSQL, providing an isolated and configurable audit log context for applications. |
GitHub repositories
This package is not used by any popular GitHub repositories.