DiagnosKit.Core
2.2.2
dotnet add package DiagnosKit.Core --version 2.2.2
NuGet\Install-Package DiagnosKit.Core -Version 2.2.2
<PackageReference Include="DiagnosKit.Core" Version="2.2.2" />
<PackageVersion Include="DiagnosKit.Core" Version="2.2.2" />
<PackageReference Include="DiagnosKit.Core" />
paket add DiagnosKit.Core --version 2.2.2
#r "nuget: DiagnosKit.Core, 2.2.2"
#:package DiagnosKit.Core@2.2.2
#addin nuget:?package=DiagnosKit.Core&version=2.2.2
#tool nuget:?package=DiagnosKit.Core&version=2.2.2
DiagnosKit.Core
A lightweight diagnostics toolkit for .NET 8+ that provides:
- Structured logging with Serilog
- Elasticsearch sink support
- Easy integration for both Web APIs and Worker Services
- Unified global exception handling (middleware)
- OpenTelemetry tracing, metrics & logging integration
- Automatic request meters (requests per endpoint, duration, failures)
- Log enrichment with CorrelationId and UserId
π¦ Installation
Install from NuGet:
dotnet add package DiagnosKit.Core
π Usage
1. Web API (Program.cs
)
using DiagnosKit.Core;
// For pre-bootstrap logging
SerilogBootstrapper.UseBootstrapLogger();
var builder = WebApplication.CreateBuilder(args);
// Configure Serilog + Elasticsearch
builder.Host.ConfigureSerilogESSink();
// Add DiagnosKit logger abstraction
builder.Services.AddLoggerManager();
// Add OpenTelemetry (traces + metrics + logging)
builder.Services.AddDiagnosKitObservability(
serviceName: "MyWebApi",
serviceVersion: "1.0.0"
);
// Optional: OTel logging
builder.Logging.AddDiagnosKitOpenTelemetryLogging();
builder.Services.AddControllers();
var app = builder.Build();
// Global exception handler
app.UseUnifiedErrorHandler();
// Log & metrics enrichment (CorrelationId + UserId + meters)
app.UseDiagnosKitLogEnricher();
app.MapControllers();
// Expose /metrics for Prometheus
app.MapPrometheusScrapingEndpoint();
// Enables Prometheus metrics collection for the application
app.UseDiagnosKitPrometheus();
app.Run();
2. Worker Service (Program.cs
)
using DiagnosKit.Core;
// Pre-bootstrap logging
SerilogBootstrapper.UseBootstrapLogger();
var builder = Host.CreateApplicationBuilder(args);
// Configure Serilog + Elasticsearch
builder.ConfigureSerilogESSink();
// Add DiagnosKit logger abstraction
builder.Services.AddLoggerManager();
// Add OpenTelemetry (traces + metrics)
builder.Services.AddDiagnosKitObservability(
serviceName: "MyWorker",
serviceVersion: "1.0.0"
);
// Optional: OTel logging
builder.Logging.AddDiagnosKitOpenTelemetryLogging();
// Register worker
builder.Services.AddHostedService<MyWorker>();
var host = builder.Build();
host.Run();
Example Worker:
public class MyWorker : BackgroundService
{
private readonly ILoggerManager _logger;
public MyWorker(ILoggerManager logger)
{
_logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_logger.LogInfo("Worker started");
while (!stoppingToken.IsCancellationRequested)
{
_logger.LogDebug("Heartbeat at: {time}", DateTimeOffset.Now);
await Task.Delay(1000, stoppingToken);
}
}
}
βοΈ Configuration
IndexFormat will default to this format: application-name-{0:yyyy.MM}, if no format is specified
In your appsettings.json
:
{
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft.AspNetCore": "Information",
"System": "Warning"
}
}
},
"ElasticSearch": {
"Url": "http://localhost:9200",
"IndexFormat": "myapp-{0:yyyy.MM}"
},
}
π Features
β Web API support (app.UseDiagnosKitErrorHandler()) β Worker Service support with ILoggerManager abstraction β Serilog + Elasticsearch integration β OpenTelemetry-ready (Tracing, Metrics, Logging) β Request meters (per endpoint: total requests, failures, duration) β Automatic log enrichment with
- CorrelationId
- UserId (when available)
- Environment
- Service
π Kibana Filtering
Because logs are enriched with structured properties (like Environment
, Service
, CorrelationId
, and Exception
),
you can filter/search logs in Kibana by:
Environment: "Production"
Service: "UserService"
Exception exists
UserId: 12345
π Kibana + Grafana
Kibana β filter structured logs by:
- Environment: "Production"
- Service: "UserService"
- CorrelationId: "abc123"
- UserId: 12345
- Exception exists
Grafana β visualize Prometheus metrics:
- http_requests_total{endpoint="/api/users"}
- http_requests_failed_total
- http_request_duration_seconds
Prometheus Setup
By default, app.MapPrometheusScrapingEndpoint() in your ASP.NET Core app exposes metrics at /metrics. In your Prometheus config (prometheus.yml), the metrics_path tells Prometheus where to scrape from. If you donβt set it, Prometheus automatically defaults to /metrics.
β Example (explicit):
scrape_configs:
- job_name: 'diagnoskit-api'
scrape_interval: 5s
metrics_path: /metrics
static_configs:
- targets: ['host.docker.internal:5000']
β Example (simpler, same result):
scrape_configs:
- job_name: 'diagnoskit-api'
scrape_interval: 5s
static_configs:
- targets: ['host.docker.internal:5000']
Run Prometheus with this config, and youβll see metrics like:
- http_server_requests_duration_seconds (per endpoint latency histograms)
- http_server_requests_count (per endpoint request count)
- .NET runtime metrics (GC, memory, threads, exceptions)
Grafana Dashboard Example
Hereβs a minimal Grafana dashboard JSON you can import to visualize key metrics:
{
"id": null,
"title": "DiagnosKit API Dashboard",
"timezone": "browser",
"panels": [
{
"type": "graph",
"title": "Request Count per Endpoint",
"targets": [
{
"expr": "sum by (method, route) (http_server_requests_count)",
"legendFormat": "{{method}} {{route}}"
}
]
},
{
"type": "graph",
"title": "Request Duration (p95)",
"targets": [
{
"expr": "histogram_quantile(0.95, sum(rate(http_server_requests_duration_seconds_bucket[5m])) by (le, route))",
"legendFormat": "{{route}}"
}
]
},
{
"type": "graph",
"title": ".NET GC Collections",
"targets": [
{
"expr": "dotnet_gc_collections_count_total",
"legendFormat": "Gen {{generation}}"
}
]
},
{
"type": "graph",
"title": "Memory Usage",
"targets": [
{
"expr": "process_private_memory_bytes",
"legendFormat": "Private Memory"
}
]
}
],
"schemaVersion": 30,
"version": 1
}
π In Grafana:
- Go to Dashboards > Import
- Paste the JSON above
- Select your Prometheus datasource
β License
This project is licensed under the MIT License - see the LICENSE file for details.
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. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- API.Common.Response.Models (>= 1.0.2)
- Microsoft.AspNetCore.Diagnostics.Abstractions (>= 2.3.0)
- Microsoft.AspNetCore.Http.Abstractions (>= 2.3.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.3)
- OpenTelemetry.Extensions.Hosting (>= 1.9.0)
- OpenTelemetry.Instrumentation.AspNetCore (>= 1.9.0)
- OpenTelemetry.Instrumentation.Http (>= 1.9.0)
- OpenTelemetry.Instrumentation.Runtime (>= 1.12.0)
- prometheus-net.AspNetCore (>= 8.2.1)
- Serilog (>= 4.3.0)
- Serilog.AspNetCore (>= 8.0.0)
- Serilog.Enrichers.ClientInfo (>= 2.3.0)
- Serilog.Enrichers.CorrelationId (>= 3.0.1)
- Serilog.Enrichers.Environment (>= 3.0.1)
- Serilog.Enrichers.Process (>= 3.0.0)
- Serilog.Enrichers.Thread (>= 4.0.0)
- Serilog.Exceptions (>= 8.4.0)
- Serilog.Sinks.Console (>= 5.0.1)
- Serilog.Sinks.Debug (>= 2.0.0)
- Serilog.Sinks.Elasticsearch (>= 9.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.