< Summary

Information
Class: SwiftCollections.Diagnostics.DiagnosticChannelLoggingExtensions
Assembly: SwiftCollections
File(s): /home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/Utility/Diagnostics/DiagnosticChannelLoggingExtensions.cs
Line coverage
96%
Covered lines: 26
Uncovered lines: 1
Coverable lines: 27
Total lines: 122
Line coverage: 96.2%
Branch coverage
92%
Covered branches: 13
Total branches: 14
Branch coverage: 92.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

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

File(s)

/home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/Utility/Diagnostics/DiagnosticChannelLoggingExtensions.cs

#LineLine coverage
 1namespace SwiftCollections.Diagnostics;
 2
 3using System;
 4using System.IO;
 5using System.Runtime.CompilerServices;
 6
 7/// <summary>
 8/// Provides interpolated diagnostic logging helpers for <see cref="DiagnosticChannel"/>.
 9/// </summary>
 10public static class DiagnosticChannelLoggingExtensions
 11{
 12    /// <summary>
 13    /// Writes an info diagnostic message without evaluating formatted expressions when info diagnostics are disabled.
 14    /// </summary>
 15    /// <param name="channel">The diagnostic channel that receives the message.</param>
 16    /// <param name="message">The interpolated diagnostic message.</param>
 17    /// <param name="source">An optional source identifier. When omitted, caller information is used.</param>
 18    /// <param name="method">The calling method name, automatically captured.</param>
 19    /// <param name="filePath">The calling file name, automatically captured to get the class name.</param>
 20    public static void Info(
 21        this DiagnosticChannel channel,
 22        [InterpolatedStringHandlerArgument("channel")] DiagnosticMessageHandler<InfoDiagnosticLevel> message,
 23        string source = "",
 24        [CallerMemberName] string method = "",
 25        [CallerFilePath] string filePath = "")
 26    {
 327        if (!message.IsEnabled)
 128            return;
 29
 230        WriteCore(channel, DiagnosticLevel.Info, message.Message, ResolveSource(source, method, filePath));
 131    }
 32
 33    /// <summary>
 34    /// Writes a warning diagnostic message without evaluating formatted expressions when warning diagnostics are disabl
 35    /// </summary>
 36    /// <param name="channel">The diagnostic channel that receives the message.</param>
 37    /// <param name="message">The interpolated diagnostic message.</param>
 38    /// <param name="source">An optional source identifier. When omitted, caller information is used.</param>
 39    /// <param name="method">The calling method name, automatically captured.</param>
 40    /// <param name="filePath">The calling file name, automatically captured to get the class name.</param>
 41    public static void Warn(
 42        this DiagnosticChannel channel,
 43        [InterpolatedStringHandlerArgument("channel")] DiagnosticMessageHandler<WarningDiagnosticLevel> message,
 44        string source = "",
 45        [CallerMemberName] string method = "",
 46        [CallerFilePath] string filePath = "")
 47    {
 148        if (!message.IsEnabled)
 049            return;
 50
 151        WriteCore(channel, DiagnosticLevel.Warning, message.Message, ResolveSource(source, method, filePath));
 152    }
 53
 54    /// <summary>
 55    /// Writes an error diagnostic message without evaluating formatted expressions when error diagnostics are disabled.
 56    /// </summary>
 57    /// <param name="channel">The diagnostic channel that receives the message.</param>
 58    /// <param name="message">The interpolated diagnostic message.</param>
 59    /// <param name="source">An optional source identifier. When omitted, caller information is used.</param>
 60    /// <param name="method">The calling method name, automatically captured.</param>
 61    /// <param name="filePath">The calling file name, automatically captured to get the class name.</param>
 62    public static void Error(
 63        this DiagnosticChannel channel,
 64        [InterpolatedStringHandlerArgument("channel")] DiagnosticMessageHandler<ErrorDiagnosticLevel> message,
 65        string source = "",
 66        [CallerMemberName] string method = "",
 67        [CallerFilePath] string filePath = "")
 68    {
 269        if (!message.IsEnabled)
 170            return;
 71
 172        WriteCore(channel, DiagnosticLevel.Error, message.Message, ResolveSource(source, method, filePath));
 173    }
 74
 75    /// <summary>
 76    /// Writes a dynamic-level diagnostic message without evaluating formatted expressions when the level is disabled.
 77    /// </summary>
 78    /// <param name="channel">The diagnostic channel that receives the message.</param>
 79    /// <param name="level">The severity level of the diagnostic event.</param>
 80    /// <param name="message">The interpolated diagnostic message.</param>
 81    /// <param name="source">An optional source identifier. When omitted, caller information is used.</param>
 82    /// <param name="method">The calling method name, automatically captured.</param>
 83    /// <param name="filePath">The calling file name, automatically captured to get the class name.</param>
 84    public static void Log(
 85        this DiagnosticChannel channel,
 86        DiagnosticLevel level,
 87        [InterpolatedStringHandlerArgument("channel", "level")] DiagnosticMessageHandler<DynamicDiagnosticLevel> message
 88        string source = "",
 89        [CallerMemberName] string method = "",
 90        [CallerFilePath] string filePath = "")
 91    {
 392        if (!message.IsEnabled)
 193            return;
 94
 295        WriteCore(channel, level, message.Message, ResolveSource(source, method, filePath));
 296    }
 97
 98    private static void WriteCore(
 99        DiagnosticChannel channel,
 100        DiagnosticLevel level,
 101        DiagnosticInterpolatedStringHandler message,
 102        string source)
 103    {
 6104        SwiftThrowHelper.ThrowIfNull(channel, nameof(channel));
 5105        channel.Write(level, message, source);
 5106    }
 107
 108    private static string ResolveSource(string source, string method, string filePath)
 109    {
 6110        if (!string.IsNullOrEmpty(source))
 1111            return source;
 112
 5113        string className = Path.GetFileNameWithoutExtension(filePath);
 5114        if (string.IsNullOrEmpty(className))
 1115            return method;
 116
 4117        if (string.IsNullOrEmpty(method))
 1118            return className;
 119
 3120        return $"{className}.{method}";
 121    }
 122}