Vali-Mediator.Observability 1.1.0

dotnet add package Vali-Mediator.Observability --version 1.1.0
                    
NuGet\Install-Package Vali-Mediator.Observability -Version 1.1.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="Vali-Mediator.Observability" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Vali-Mediator.Observability" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Vali-Mediator.Observability" />
                    
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 Vali-Mediator.Observability --version 1.1.0
                    
#r "nuget: Vali-Mediator.Observability, 1.1.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 Vali-Mediator.Observability@1.1.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=Vali-Mediator.Observability&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Vali-Mediator.Observability&version=1.1.0
                    
Install as a Cake Tool

Vali-Mediator.Observability

Zero-dependency observability integration for Vali-Mediator (.NET 7 / 8 / 9).

Provides OpenTelemetry-compatible ActivitySource tracing, pluggable metrics via IMetricsCollector, and structured per-request lifecycle hooks via IRequestObserver — with no OpenTelemetry SDK dependency required.


Installation

dotnet add package Vali-Mediator.Observability

Quick Start

// Program.cs
builder.Services.AddValiMediator(config =>
{
    config.RegisterServicesFromAssemblyContaining<Program>();
    config.AddObservabilityBehavior();   // registers both request + dispatch behaviors
});

builder.Services
    .AddObservability()                  // registers NoOpMetricsCollector (replace below)
    .AddConsoleMetrics()                 // replace with ConsoleMetricsCollector (dev only)
    .AddConsoleLoggingObserver();        // add ConsoleLoggingObserver (dev only)

Features

Feature API
OpenTelemetry-compatible tracing ValiMediatorDiagnostics.ActivitySource — source name "Vali-Mediator"
Request lifecycle hooks IRequestObserverOnStarted, OnCompleted, OnFailed
Rich execution context ObservabilityContext — name, operationId, duration, success, exception, request, response, tags
Pluggable metrics IMetricsCollectorRecordRequestStarted, RecordRequestCompleted, RecordRequestFailed
No-op default NoOpMetricsCollector — zero overhead when no metrics back-end is configured
Console output ConsoleMetricsCollector + ConsoleLoggingObserver for dev/debug
IRequest pipeline ObservabilityBehavior<TRequest, TResponse>
IDispatch pipeline ObservabilityDispatchBehavior<TRequest> (INotification / IFireAndForget)
Multiple observers All observers always run; exceptions collected into AggregateException
Fluent DI AddObservabilityBehavior, AddObservability, AddMetricsCollector<T>, AddRequestObserver<T>, AddConsoleMetrics, AddConsoleLoggingObserver

OpenTelemetry Integration

No additional packages required inside this library. To enable tracing, add the source to your OpenTelemetry tracer provider:

builder.Services.AddOpenTelemetry()
    .WithTracing(b => b
        .AddSource("Vali-Mediator")
        .AddOtlpExporter());

Custom Metrics Collector

Implement IMetricsCollector and register it:

public sealed class PrometheusMetricsCollector : IMetricsCollector
{
    private static readonly Counter RequestsStarted =
        Metrics.CreateCounter("vali_mediator_requests_started_total", "Total started requests", "request");

    public void RecordRequestStarted(string requestName) =>
        RequestsStarted.WithLabels(requestName).Inc();

    public void RecordRequestCompleted(string requestName, TimeSpan duration, bool success) { /* ... */ }
    public void RecordRequestFailed(string requestName, TimeSpan duration, string exceptionType) { /* ... */ }
}

// Registration:
builder.Services
    .AddObservability()
    .AddMetricsCollector<PrometheusMetricsCollector>();

Custom Observer

public sealed class AuditObserver : IRequestObserver
{
    private readonly IAuditService _audit;
    public AuditObserver(IAuditService audit) => _audit = audit;

    public Task OnStarted(ObservabilityContext ctx, CancellationToken ct = default) =>
        _audit.LogStartAsync(ctx.RequestName, ctx.OperationId, ct);

    public Task OnCompleted(ObservabilityContext ctx, CancellationToken ct = default) =>
        _audit.LogCompletedAsync(ctx.RequestName, ctx.Duration, ct);

    public Task OnFailed(ObservabilityContext ctx, CancellationToken ct = default) =>
        _audit.LogFailedAsync(ctx.RequestName, ctx.Exception, ct);
}

// Registration (multiple observers supported):
builder.Services.AddRequestObserver<AuditObserver>();

ObservabilityContext Properties

Property Type Description
RequestName string typeof(TRequest).Name
OperationId string? Unique Guid per execution
StartedAt DateTimeOffset UTC start timestamp
Duration TimeSpan? Set after handler returns
IsSuccess bool true on success
Exception Exception? Set on failure
Request object? The original request object
Response object? The response (set on success)
Tags Dictionary<string, object?> Extensible metadata bag

Donations

If Vali-Mediator is useful to you, consider supporting its development:


License

Apache License 2.0

Contributions

Issues and pull requests are welcome on GitHub.

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  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 is compatible.  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.1.0 102 4/13/2026
1.0.1 105 3/18/2026
1.0.0 104 3/16/2026

v1.1.0 — Package structure: now uses Vali-Mediator as NuGet dependency instead of local ProjectReference.

           v1.0.1 — Documentation: enabled XML documentation file for full IntelliSense support.

           v1.0.0 — Initial release:
           - ActivitySource "Vali-Mediator" (OpenTelemetry-compatible, no otel dependency).
           - IRequestObserver lifecycle hooks: OnStarted, OnCompleted, OnFailed.
           - ObservabilityContext: rich per-execution context with Tags dictionary.
           - IMetricsCollector pluggable interface with NoOpMetricsCollector and ConsoleMetricsCollector.
           - ObservabilityBehavior for IRequest pipeline.
           - ObservabilityDispatchBehavior for INotification / IFireAndForget pipeline.
           - ConsoleLoggingObserver for structured console output.
           - Fluent DI extensions: AddObservabilityBehavior, AddObservability, AddMetricsCollector, AddRequestObserver, AddConsoleMetrics, AddConsoleLoggingObserver.