DotNetObservabilityLite 1.0.0

dotnet add package DotNetObservabilityLite --version 1.0.0
                    
NuGet\Install-Package DotNetObservabilityLite -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DotNetObservabilityLite" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DotNetObservabilityLite" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="DotNetObservabilityLite" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DotNetObservabilityLite --version 1.0.0
                    
#r "nuget: DotNetObservabilityLite, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package DotNetObservabilityLite@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DotNetObservabilityLite&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=DotNetObservabilityLite&version=1.0.0
                    
Install as a Cake Tool

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

  1. Add correlation ID middleware first - before other middleware
  2. Use LogInformationSafe() for any log that might contain PII
  3. Enable latency tracking to identify slow requests
  4. Include correlation ID in custom log messages for tracing
  5. Filter PII before logging using PiiFilter or safe logging methods
  6. Monitor slow request warnings to identify performance issues
  7. Pass correlation ID to downstream services in headers

API Reference

  • CorrelationIdMiddleware - Middleware for correlation ID tracking
  • UseCorrelationId() - Extension method to enable correlation IDs
  • GetCorrelationId() - Get correlation ID from HttpContext
  • PiiFilter - Filter PII from log messages
  • LogInformationSafe() - Log information with PII filtering
  • LogErrorSafe() - Log errors with PII filtering
  • LatencyTrackerMiddleware - Middleware for latency tracking
  • UseLatencyTracking() - Extension method to enable latency tracking
Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.