DotNetObservabilityLite 1.0.0
dotnet add package DotNetObservabilityLite --version 1.0.0
NuGet\Install-Package DotNetObservabilityLite -Version 1.0.0
<PackageReference Include="DotNetObservabilityLite" Version="1.0.0" />
<PackageVersion Include="DotNetObservabilityLite" Version="1.0.0" />
<PackageReference Include="DotNetObservabilityLite" />
paket add DotNetObservabilityLite --version 1.0.0
#r "nuget: DotNetObservabilityLite, 1.0.0"
#:package DotNetObservabilityLite@1.0.0
#addin nuget:?package=DotNetObservabilityLite&version=1.0.0
#tool nuget:?package=DotNetObservabilityLite&version=1.0.0
DotNetObservabilityLite
Logging and observability helpers for correlation IDs, PII filtering, latency tracking, and business metrics.
Problems Solved
- No correlation IDs: Can't trace requests across services and logs
- Logs too noisy: Too much logging, hard to find relevant information
- PII leaking in logs: Personal information (emails, SSNs) logged accidentally
- No latency tracking: Don't know which requests are slow
- No business metrics: Can't track business-level metrics (orders per hour, etc.)
Installation
dotnet add package DotNetObservabilityLite
Quick Start
1. Correlation IDs
Problem: Can't trace a request across multiple services and log entries.
using DotNetObservabilityLite;
// ✅ GOOD: Add correlation ID middleware
app.UseCorrelationId(options =>
{
options.RequestHeader = "X-Correlation-Id";
options.ResponseHeader = "X-Correlation-Id";
});
// Correlation ID automatically added to:
// - Response headers
// - Logging scope (all logs include correlation ID)
// Get correlation ID in your code
var correlationId = context.GetCorrelationId();
logger.LogInformation("Processing request {CorrelationId}", correlationId);
2. PII Filtering
Problem: Personal information (emails, SSNs, credit cards) logged accidentally.
// ❌ BAD: PII logged in plain text
logger.LogInformation("User {Email} logged in", user.Email);
// Logs: "User john.doe@example.com logged in"
// ✅ GOOD: PII automatically filtered
logger.LogInformationSafe("User {Email} logged in", user.Email);
// Logs: "User *** logged in"
// Or use PiiFilter directly
var filter = new PiiFilter();
var safeMessage = filter.Filter($"Contact user at {user.Email} or call {user.Phone}");
// Returns: "Contact user at *** or call ***"
3. Latency Tracking
Problem: Don't know which requests are slow or causing performance issues.
// ✅ GOOD: Add latency tracking middleware
app.UseLatencyTracking();
// Automatically:
// - Tracks request latency
// - Adds X-Response-Time header
// - Logs latency for each request
// - Logs warning for slow requests (>1 second)
// Response includes:
// Headers:
// X-Response-Time: 245ms
// Logs:
// "Request GET /api/orders completed in 245ms with status 200"
// "Slow request detected: POST /api/orders took 1250ms" (if >1s)
4. Combined Observability
Problem: Need correlation IDs, PII filtering, and latency tracking together.
// ✅ GOOD: Add all observability middleware
var app = builder.Build();
// Order matters - correlation ID should be first
app.UseCorrelationId();
app.UseLatencyTracking();
// Now all requests have:
// - Correlation ID in headers and logs
// - Latency tracking with warnings for slow requests
// - PII filtering available via LogInformationSafe()
Real-World Example
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddHttpContextAccessor();
var app = builder.Build();
// Add observability middleware (order matters)
app.UseCorrelationId(options =>
{
options.RequestHeader = "X-Correlation-Id";
options.ResponseHeader = "X-Correlation-Id";
});
app.UseLatencyTracking();
app.MapControllers();
app.Run();
}
}
// Controller with observability
[ApiController]
[Route("api/[controller]")]
public class OrdersController : ControllerBase
{
private readonly IOrderService _orderService;
private readonly ILogger<OrdersController> _logger;
private readonly IHttpContextAccessor _httpContextAccessor;
[HttpPost]
public async Task<ActionResult<Order>> CreateOrder(CreateOrderRequest request)
{
// Get correlation ID for tracing
var correlationId = _httpContextAccessor.HttpContext?.GetCorrelationId();
_logger.LogInformation(
"Creating order {CorrelationId}",
correlationId
);
try
{
// Use safe logging to filter PII
_logger.LogInformationSafe(
"Creating order for user {Email}",
request.CustomerEmail
);
var order = await _orderService.CreateOrderAsync(request);
_logger.LogInformation(
"Order {OrderId} created successfully {CorrelationId}",
order.Id,
correlationId
);
return Ok(order);
}
catch (Exception ex)
{
// Exception logged with correlation ID automatically
_logger.LogErrorSafe(
ex,
"Failed to create order for {Email} {CorrelationId}",
request.CustomerEmail,
correlationId
);
throw;
}
}
}
// Service with observability
public class OrderService
{
private readonly ILogger<OrderService> _logger;
private readonly IHttpContextAccessor _httpContextAccessor;
public async Task<Order> CreateOrderAsync(CreateOrderRequest request)
{
var correlationId = _httpContextAccessor.HttpContext?.GetCorrelationId();
_logger.LogInformation(
"Processing order creation {CorrelationId}",
correlationId
);
// Business logic...
_logger.LogInformation(
"Order creation completed {CorrelationId}",
correlationId
);
return order;
}
}
PII Filtering Patterns
// Default patterns filtered:
// - Email addresses: user@example.com → ***
// - Credit cards: 1234-5678-9012-3456 → ***
// - SSN: 123-45-6789 → ***
// - Phone numbers: 555-123-4567 → ***
// Add custom patterns
var filter = new PiiFilter();
filter.AddPattern(@"\b\d{4}-\d{4}-\d{4}-\d{4}\b", "****"); // Custom pattern
// Filter before logging
var safeMessage = filter.Filter($"User {user.Email} with card {cardNumber}");
logger.LogInformation(safeMessage);
Logging with Correlation IDs
All logs automatically include correlation ID in scope:
[2024-01-15 10:30:45] [Information] [CorrelationId: abc123-def456] Creating order
[2024-01-15 10:30:45] [Information] [CorrelationId: abc123-def456] Processing payment
[2024-01-15 10:30:46] [Information] [CorrelationId: abc123-def456] Order created successfully
Latency Tracking Output
[2024-01-15 10:30:45] [Information] Request GET /api/orders completed in 245ms with status 200
[2024-01-15 10:30:50] [Warning] Slow request detected: POST /api/orders took 1250ms
Best Practices
- Add correlation ID middleware first - before other middleware
- Use
LogInformationSafe()for any log that might contain PII - Enable latency tracking to identify slow requests
- Include correlation ID in custom log messages for tracing
- Filter PII before logging using
PiiFilteror safe logging methods - Monitor slow request warnings to identify performance issues
- Pass correlation ID to downstream services in headers
API Reference
CorrelationIdMiddleware- Middleware for correlation ID trackingUseCorrelationId()- Extension method to enable correlation IDsGetCorrelationId()- Get correlation ID from HttpContextPiiFilter- Filter PII from log messagesLogInformationSafe()- Log information with PII filteringLogErrorSafe()- Log errors with PII filteringLatencyTrackerMiddleware- Middleware for latency trackingUseLatencyTracking()- Extension method to enable latency tracking
| 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
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Logging (>= 8.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.0 | 124 | 12/30/2025 |
Initial release. See README for details.