TNTKronosys.Monitoring 1.0.0
TNTKronosys.Monitoring
A lightweight ASP.NET Core library for exposing standardized health polling endpoints. When integrated into your project, it provides GET /api/health/polling which returns health data in the shape expected by the main TNTMonitoring-Backend polling service.
Features
- Standardized Health Endpoint —
GET /api/health/pollingexposed automatically - Extensible Health Checks — Implement
IHealthDataProviderto define your application's health logic - Dependency Tracking — Report status of databases, external APIs, caches, and other dependencies
- Performance Metrics — Include response time, version, and detailed status messages
- Simple Integration — Two lines of code in
Program.csto enable full functionality
Quick Start
1. Add the NuGet Package
(Add TNTKronosys.Monitoring to your project)
2. Implement IHealthDataProvider
public class YourProjectHealthProvider : IHealthDataProvider
{
private readonly YourDbContext _dbContext;
private readonly IHttpClientFactory _httpClientFactory;
public YourProjectHealthProvider(
YourDbContext dbContext,
IHttpClientFactory httpClientFactory)
{
_dbContext = dbContext;
_httpClientFactory = httpClientFactory;
}
public async Task<PollingHealthResponse> GetHealthAsync()
{
var startTime = DateTime.UtcNow;
var dependencies = new List<DependencyStatus>();
var overallStatus = "Healthy";
// Check Database
try
{
await _dbContext.Database.ExecuteSqlRawAsync("SELECT 1");
dependencies.Add(new DependencyStatus
{
Name = "Database",
Status = "Healthy"
});
}
catch (Exception ex)
{
dependencies.Add(new DependencyStatus
{
Name = "Database",
Status = "Unhealthy",
Details = "Connection check failed"
});
overallStatus = "Unhealthy";
}
// Add more dependency checks as needed...
var responseTime = (DateTime.UtcNow - startTime).TotalMilliseconds;
return new PollingHealthResponse
{
Status = overallStatus,
ResponseTime = responseTime,
Timestamp = DateTime.UtcNow,
Dependencies = dependencies,
Message = overallStatus == "Healthy"
? "All systems operational"
: "System experiencing issues"
};
}
}
3. Register in Program.cs
// Register your health provider
builder.Services.AddScoped<IHealthDataProvider, YourProjectHealthProvider>();
// Ensure these are present (if not already)
builder.Services.AddControllers();
builder.Services.AddHttpClient();
// Register the health polling library
builder.Services.AddHealthPolling();
var app = builder.Build();
// Ensure this is present (if not already)
app.MapControllers();
app.Run();
4. Done!
Your API now exposes GET /api/health/polling. The main TNTMonitoring-Backend can call this endpoint to get your application's health status.
Response Format
{
"status": "Healthy",
"responseTime": 45.23,
"timestamp": "2024-01-15T10:30:45.1234567Z",
"dependencies": [
{
"name": "Database",
"status": "Healthy",
"details": null
}
],
"message": "All systems operational"
}
Response Behaviour
- The endpoint always returns HTTP 200 when the check completes, including when the application is unhealthy. The health state is carried in the body.
statusis the source of truth:Healthy,DegradedorUnhealthy. The TNTMonitoring polling service marks an endpoint up forHealthyandDegraded, and down forUnhealthyor a non-success HTTP response.versionis returned at the top level. SetVersionin your provider (for example from your assembly's informational version) and set the endpoint's version field path toversionin TNTMonitoring.- If
GetHealthAsyncthrows, the endpoint logs the exception and returnsUnhealthywith no dependencies. - The endpoint is anonymous. Do not put exception messages, connection strings or hostnames in
Details; use short, generic messages and log the full exception yourself.
Configuration
// Default: GET /api/health/polling
builder.Services.AddHealthPolling();
// Custom route prefix: GET /monitoring/health/polling
builder.Services.AddHealthPolling(options => options.Route = "monitoring/health");
Routedefaults toapi/health. Leading and trailing slashes are ignored, and an empty route throws anArgumentException.AddHealthPolling()can be called before or after registeringIHealthDataProvider. The provider is resolved when the application starts, and startup fails with a clear message if it cannot be resolved.
Core Types
PollingHealthResponse
The standardized response returned by /api/health/polling:
Status(string) — "Healthy", "Degraded", or "Unhealthy"ResponseTime(double) — milliseconds for the health checkTimestamp(DateTime) — UTC when the check was performedDependencies(List) — status of each dependency Message(string?) — optional summary message
DependencyStatus
Represents the health of a single dependency:
Name(string) — e.g., "Database", "ExternalAPI"Status(string) — "Healthy", "Degraded", or "Unhealthy"Details(string?) — optional error message or details
IHealthDataProvider
Interface you implement to provide health data:
Task<PollingHealthResponse> GetHealthAsync();
Documentation
For detailed implementation examples including PostgreSQL, MongoDB, hybrid databases, and external API integration, see IMPLEMENTATION_GUIDE.md.
Architecture
The library follows a simple, extensible architecture:
IHealthDataProvider (your implementation)
↓
GetHealthAsync()
↓
PollingHealthResponse (standardized DTO)
↓
HealthPollingController
↓
GET /api/health/polling
Each consuming application:
- Implements
IHealthDataProviderwith their own health logic - Registers it in their
Program.cs - Calls
AddHealthPolling()to enable the endpoint - The main backend polls the endpoint for standardized health data
Building & Testing
Build the Library
dotnet build TNTKronosys.Monitoring/TNTKronosys.Monitoring.csproj
Test the Endpoint Locally
curl -X GET "http://localhost:5000/api/health/polling"
Integrate into Your Project
// In Program.cs
builder.Services.AddScoped<IHealthDataProvider, YourHealthProvider>();
builder.Services.AddHealthPolling();
Dependencies
- ASP.NET Core shared framework (
Microsoft.AspNetCore.App) — referenced viaFrameworkReference
License
See repository for license information.
Support
For issues or questions, refer to the main TNTMonitoring-Backend repository or contact the development team.
No packages depend on TNTKronosys.Monitoring.
.NET 8.0
- No dependencies.