< Summary

Information
Class: Chronicler.Diagnostics.DiagnosticChannel
Assembly: Chronicler.Diagnostics
File(s): /home/runner/work/Chronicler/Chronicler/src/Chronicler.Diagnostics/DiagnosticChannel.cs
Line coverage
100%
Covered lines: 19
Uncovered lines: 0
Coverable lines: 19
Total lines: 82
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
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%
.ctor(...)100%22100%
get_Sink()100%11100%
set_Sink(...)100%22100%
IsEnabled(...)100%44100%
Write(...)100%22100%
Write(...)100%22100%

File(s)

/home/runner/work/Chronicler/Chronicler/src/Chronicler.Diagnostics/DiagnosticChannel.cs

#LineLine coverage
 1//=======================================================================
 2// DiagnosticChannel.cs
 3//=======================================================================
 4// MIT License, Copyright (c) 2024-present David Oravsky (mrdav30)
 5// See LICENSE file in the project root for full license information.
 6//=======================================================================
 7
 8namespace Chronicler.Diagnostics;
 9
 10using System.Runtime.CompilerServices;
 11
 12/// <summary>Routes diagnostics at enabled levels to a sink.</summary>
 13/// <remarks>The sink runs synchronously on the caller's thread. The channel starts disabled with a no-op sink.</remarks
 14public sealed class DiagnosticChannel
 15{
 116    private static readonly DiagnosticSink NoopSink = static (in DiagnosticEvent _) => { };
 17    private DiagnosticSink _sink;
 18
 19    /// <summary>Creates a disabled channel.</summary>
 20    /// <param name="name">The event channel name. Null, empty, or whitespace selects <c>Default</c>.</param>
 4621    public DiagnosticChannel(string name)
 22    {
 4623        Name = string.IsNullOrWhiteSpace(name) ? "Default" : name;
 4624        _sink = NoopSink;
 4625        MinimumLevel = DiagnosticLevel.None;
 4626    }
 27
 28    /// <summary>Gets the channel label.</summary>
 29    /// <value>The normalized name included in every event emitted by this channel.</value>
 30    public string Name { get; }
 31
 32    /// <summary>Gets or sets the severity threshold.</summary>
 33    /// <value>The lowest enabled level. The default, <see cref="DiagnosticLevel.None"/>, disables the channel.</value>
 34    public DiagnosticLevel MinimumLevel { get; set; }
 35
 36    /// <summary>Gets or sets the dispatch callback.</summary>
 37    /// <value>The retained delegate invoked for emitted events. Assign null to restore the no-op sink.</value>
 38    public DiagnosticSink Sink
 39    {
 240        get => _sink;
 2941        set => _sink = value ?? NoopSink;
 42    }
 43
 44    /// <summary>Checks the channel's current severity threshold.</summary>
 45    /// <param name="level">The severity being considered. <see cref="DiagnosticLevel.None"/> is always disabled.</param
 46    /// <returns>True when the channel is enabled and the severity reaches its threshold.</returns>
 47    public bool IsEnabled(DiagnosticLevel level)
 48    {
 26649        return MinimumLevel != DiagnosticLevel.None
 26650            && level >= MinimumLevel
 26651            && level != DiagnosticLevel.None;
 52    }
 53
 54    /// <summary>Emits an already formatted event when the level is enabled.</summary>
 55    /// <param name="level">The severity tested against the current threshold.</param>
 56    /// <param name="message">The message text, formatted by the caller before this call.</param>
 57    /// <param name="source">An optional origin label copied into the event, or an empty string when omitted.</param>
 58    /// <remarks>Invokes the sink inline. Exceptions from the sink propagate to the caller.</remarks>
 59    public void Write(DiagnosticLevel level, string message, string source = "")
 60    {
 1761        if (!IsEnabled(level))
 462            return;
 63
 1364        _sink(new DiagnosticEvent(Name, level, message, source));
 1365    }
 66
 67    /// <summary>Emits an interpolated event using the handler's enabled decision.</summary>
 68    /// <param name="level">The event severity used when creating the handler.</param>
 69    /// <param name="message">The handler whose captured gate controls delivery and skips interpolation when disabled.</
 70    /// <param name="source">An optional origin label copied into the event, or an empty string when omitted.</param>
 71    /// <remarks>Invokes the sink inline. Exceptions from the sink propagate to the caller.</remarks>
 72    public void Write(
 73        DiagnosticLevel level,
 74        [InterpolatedStringHandlerArgument("", nameof(level))] DiagnosticInterpolatedStringHandler message,
 75        string source = "")
 76    {
 877        if (!message.IsEnabled)
 178            return;
 79
 780        _sink(new DiagnosticEvent(Name, level, message.GetFormattedText(), source));
 781    }
 82}