< Summary

Information
Class: Chronicler.Diagnostics.DiagnosticChannelLoggingExtensions
Assembly: Chronicler.Diagnostics
File(s): /home/runner/work/Chronicler/Chronicler/src/Chronicler.Diagnostics/DiagnosticChannelLoggingExtensions.cs
Line coverage
100%
Covered lines: 35
Uncovered lines: 0
Coverable lines: 35
Total lines: 212
Line coverage: 100%
Branch coverage
100%
Covered branches: 16
Total branches: 16
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Info(...)100%11100%
Warn(...)100%11100%
Error(...)100%11100%
Log(...)100%22100%
Info(...)100%22100%
Warn(...)100%22100%
Error(...)100%22100%
Log(...)100%22100%
WriteCore(...)100%11100%
ResolveSource(...)100%66100%

File(s)

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

#LineLine coverage
 1//=======================================================================
 2// DiagnosticChannelLoggingExtensions.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.IO;
 11using System.Runtime.CompilerServices;
 12
 13/// <summary>
 14/// Emits fixed- or dynamic-level messages through a <see cref="DiagnosticChannel"/>.
 15/// </summary>
 16/// <remarks>
 17/// A nonempty source takes precedence over caller information. An empty source combines the caller file stem
 18/// and member name when available. Sinks run inline and their exceptions propagate.
 19/// </remarks>
 20public static class DiagnosticChannelLoggingExtensions
 21{
 22    /// <summary>
 23    /// Writes an already formatted info message when info diagnostics are enabled.
 24    /// </summary>
 25    /// <param name="channel">The diagnostic channel that receives the message.</param>
 26    /// <param name="message">The already formatted diagnostic message.</param>
 27    /// <param name="source">The explicit source label. Empty or omitted selects the caller-derived source.</param>
 28    /// <param name="method">The captured caller member name used in the fallback source.</param>
 29    /// <param name="filePath">The captured caller path. The file name without its extension supplies the first part of 
 30    /// <exception cref="System.ArgumentNullException">The channel is null.</exception>
 31    public static void Info(
 32        this DiagnosticChannel channel,
 33        string message,
 34        string source = "",
 35        [CallerMemberName] string method = "",
 36        [CallerFilePath] string filePath = "") =>
 437        Log(channel, DiagnosticLevel.Info, message, source, method, filePath);
 38
 39    /// <summary>
 40    /// Writes an already formatted warning message when warning diagnostics are enabled.
 41    /// </summary>
 42    /// <param name="channel">The diagnostic channel that receives the message.</param>
 43    /// <param name="message">The already formatted diagnostic message.</param>
 44    /// <param name="source">The explicit source label. Empty or omitted selects the caller-derived source.</param>
 45    /// <param name="method">The captured caller member name used in the fallback source.</param>
 46    /// <param name="filePath">The captured caller path. The file name without its extension supplies the first part of 
 47    /// <exception cref="System.ArgumentNullException">The channel is null.</exception>
 48    public static void Warn(
 49        this DiagnosticChannel channel,
 50        string message,
 51        string source = "",
 52        [CallerMemberName] string method = "",
 53        [CallerFilePath] string filePath = "") =>
 454        Log(channel, DiagnosticLevel.Warning, message, source, method, filePath);
 55
 56    /// <summary>
 57    /// Writes an already formatted error message when error diagnostics are enabled.
 58    /// </summary>
 59    /// <param name="channel">The diagnostic channel that receives the message.</param>
 60    /// <param name="message">The already formatted diagnostic message.</param>
 61    /// <param name="source">The explicit source label. Empty or omitted selects the caller-derived source.</param>
 62    /// <param name="method">The captured caller member name used in the fallback source.</param>
 63    /// <param name="filePath">The captured caller path. The file name without its extension supplies the first part of 
 64    /// <exception cref="System.ArgumentNullException">The channel is null.</exception>
 65    public static void Error(
 66        this DiagnosticChannel channel,
 67        string message,
 68        string source = "",
 69        [CallerMemberName] string method = "",
 70        [CallerFilePath] string filePath = "") =>
 471        Log(channel, DiagnosticLevel.Error, message, source, method, filePath);
 72
 73    /// <summary>
 74    /// Writes an already formatted message when the requested diagnostic level is enabled.
 75    /// </summary>
 76    /// <param name="channel">The diagnostic channel that receives the message.</param>
 77    /// <param name="level">The severity level of the diagnostic event.</param>
 78    /// <param name="message">The already formatted diagnostic message.</param>
 79    /// <param name="source">The explicit source label. Empty or omitted selects the caller-derived source.</param>
 80    /// <param name="method">The captured caller member name used in the fallback source.</param>
 81    /// <param name="filePath">The captured caller path. The file name without its extension supplies the first part of 
 82    /// <exception cref="System.ArgumentNullException">The channel is null.</exception>
 83    public static void Log(
 84        this DiagnosticChannel channel,
 85        DiagnosticLevel level,
 86        string message,
 87        string source = "",
 88        [CallerMemberName] string method = "",
 89        [CallerFilePath] string filePath = "")
 90    {
 1691        ChronicleGuard.ThrowIfNull(channel, nameof(channel));
 1292        if (!channel.IsEnabled(level))
 493            return;
 94
 895        channel.Write(level, message, ResolveSource(source, method, filePath));
 896    }
 97
 98    /// <summary>
 99    /// Writes an info diagnostic message without evaluating formatted expressions when info diagnostics are disabled.
 100    /// </summary>
 101    /// <param name="channel">The diagnostic channel that receives the message.</param>
 102    /// <param name="message">The interpolated diagnostic message.</param>
 103    /// <param name="source">The explicit source label. Empty or omitted selects the caller-derived source.</param>
 104    /// <param name="method">The captured caller member name used in the fallback source.</param>
 105    /// <param name="filePath">The captured caller path. The file name without its extension supplies the first part of 
 106    /// <exception cref="System.ArgumentNullException">The channel is null.</exception>
 107    public static void Info(
 108        this DiagnosticChannel channel,
 109        [InterpolatedStringHandlerArgument("channel")] DiagnosticMessageHandler<InfoDiagnosticLevel> message,
 110        string source = "",
 111        [CallerMemberName] string method = "",
 112        [CallerFilePath] string filePath = "")
 113    {
 4114        if (!message.IsEnabled)
 2115            return;
 116
 2117        WriteCore(channel, DiagnosticLevel.Info, message.Message, ResolveSource(source, method, filePath));
 1118    }
 119
 120    /// <summary>
 121    /// Writes a warning diagnostic message without evaluating formatted expressions when warning diagnostics are disabl
 122    /// </summary>
 123    /// <param name="channel">The diagnostic channel that receives the message.</param>
 124    /// <param name="message">The interpolated diagnostic message.</param>
 125    /// <param name="source">The explicit source label. Empty or omitted selects the caller-derived source.</param>
 126    /// <param name="method">The captured caller member name used in the fallback source.</param>
 127    /// <param name="filePath">The captured caller path. The file name without its extension supplies the first part of 
 128    /// <exception cref="System.ArgumentNullException">The channel is null.</exception>
 129    public static void Warn(
 130        this DiagnosticChannel channel,
 131        [InterpolatedStringHandlerArgument("channel")] DiagnosticMessageHandler<WarningDiagnosticLevel> message,
 132        string source = "",
 133        [CallerMemberName] string method = "",
 134        [CallerFilePath] string filePath = "")
 135    {
 203136        if (!message.IsEnabled)
 201137            return;
 138
 2139        WriteCore(channel, DiagnosticLevel.Warning, message.Message, ResolveSource(source, method, filePath));
 2140    }
 141
 142    /// <summary>
 143    /// Writes an error diagnostic message without evaluating formatted expressions when error diagnostics are disabled.
 144    /// </summary>
 145    /// <param name="channel">The diagnostic channel that receives the message.</param>
 146    /// <param name="message">The interpolated diagnostic message.</param>
 147    /// <param name="source">The explicit source label. Empty or omitted selects the caller-derived source.</param>
 148    /// <param name="method">The captured caller member name used in the fallback source.</param>
 149    /// <param name="filePath">The captured caller path. The file name without its extension supplies the first part of 
 150    /// <exception cref="System.ArgumentNullException">The channel is null.</exception>
 151    public static void Error(
 152        this DiagnosticChannel channel,
 153        [InterpolatedStringHandlerArgument("channel")] DiagnosticMessageHandler<ErrorDiagnosticLevel> message,
 154        string source = "",
 155        [CallerMemberName] string method = "",
 156        [CallerFilePath] string filePath = "")
 157    {
 3158        if (!message.IsEnabled)
 2159            return;
 160
 1161        WriteCore(channel, DiagnosticLevel.Error, message.Message, ResolveSource(source, method, filePath));
 1162    }
 163
 164    /// <summary>
 165    /// Writes a dynamic-level diagnostic message without evaluating formatted expressions when the level is disabled.
 166    /// </summary>
 167    /// <param name="channel">The diagnostic channel that receives the message.</param>
 168    /// <param name="level">The severity level of the diagnostic event.</param>
 169    /// <param name="message">The interpolated diagnostic message.</param>
 170    /// <param name="source">The explicit source label. Empty or omitted selects the caller-derived source.</param>
 171    /// <param name="method">The captured caller member name used in the fallback source.</param>
 172    /// <param name="filePath">The captured caller path. The file name without its extension supplies the first part of 
 173    /// <exception cref="System.ArgumentNullException">The channel is null.</exception>
 174    public static void Log(
 175        this DiagnosticChannel channel,
 176        DiagnosticLevel level,
 177        [InterpolatedStringHandlerArgument("channel", "level")] DiagnosticMessageHandler<DynamicDiagnosticLevel> message
 178        string source = "",
 179        [CallerMemberName] string method = "",
 180        [CallerFilePath] string filePath = "")
 181    {
 4182        if (!message.IsEnabled)
 2183            return;
 184
 2185        WriteCore(channel, level, message.Message, ResolveSource(source, method, filePath));
 2186    }
 187
 188    private static void WriteCore(
 189        DiagnosticChannel channel,
 190        DiagnosticLevel level,
 191        DiagnosticInterpolatedStringHandler message,
 192        string source)
 193    {
 7194        ChronicleGuard.ThrowIfNull(channel, nameof(channel));
 6195        channel.Write(level, message, source);
 6196    }
 197
 198    private static string ResolveSource(string source, string method, string filePath)
 199    {
 15200        if (!string.IsNullOrEmpty(source))
 2201            return source;
 202
 13203        string className = Path.GetFileNameWithoutExtension(filePath);
 13204        if (string.IsNullOrEmpty(className))
 1205            return method;
 206
 12207        if (string.IsNullOrEmpty(method))
 1208            return className;
 209
 11210        return $"{className}.{method}";
 211    }
 212}