< Summary

Information
Class: NexMediator.Abstractions.Context.CorrelationContext
Assembly: NexMediator.Abstractions
File(s): /home/runner/work/NexMediator/NexMediator/src/NexMediator.Abstractions/Context/CorrelationContext.cs
Line coverage
100%
Covered lines: 6
Uncovered lines: 0
Coverable lines: 6
Total lines: 28
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
get_CorrelationId()100%22100%
SetCorrelationId(...)100%22100%

File(s)

/home/runner/work/NexMediator/NexMediator/src/NexMediator.Abstractions/Context/CorrelationContext.cs

#LineLine coverage
 1namespace NexMediator.Abstractions.Context;
 2
 3/// <summary>
 4/// Default implementation of ICorrelationContext using AsyncLocal.
 5/// </summary>
 6/// <remarks>
 7/// Stores a unique Correlation ID per async flow (like a request).
 8/// This helps to track and group logs from the same operation.
 9/// </remarks>
 10public class CorrelationContext : ICorrelationContext
 11{
 112    private static readonly AsyncLocal<string?> _current = new();
 13
 14    /// <summary>
 15    /// Gets the current Correlation ID. Throws if not yet set.
 16    /// </summary>
 17    public string CorrelationId =>
 618        _current.Value ?? throw new InvalidOperationException("Correlation ID has not been set.");
 19
 20    /// <summary>
 21    /// Sets the Correlation ID only if not already defined.
 22    /// </summary>
 23    public void SetCorrelationId(string id)
 424    {
 425        if (_current.Value is null)
 426            _current.Value = id;
 427    }
 28}