| | | 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 | | |
| | | 8 | | namespace Chronicler.Diagnostics; |
| | | 9 | | |
| | | 10 | | using System.IO; |
| | | 11 | | using 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> |
| | | 20 | | public 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 = "") => |
| | 4 | 37 | | 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 = "") => |
| | 4 | 54 | | 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 = "") => |
| | 4 | 71 | | 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 | | { |
| | 16 | 91 | | ChronicleGuard.ThrowIfNull(channel, nameof(channel)); |
| | 12 | 92 | | if (!channel.IsEnabled(level)) |
| | 4 | 93 | | return; |
| | | 94 | | |
| | 8 | 95 | | channel.Write(level, message, ResolveSource(source, method, filePath)); |
| | 8 | 96 | | } |
| | | 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 | | { |
| | 4 | 114 | | if (!message.IsEnabled) |
| | 2 | 115 | | return; |
| | | 116 | | |
| | 2 | 117 | | WriteCore(channel, DiagnosticLevel.Info, message.Message, ResolveSource(source, method, filePath)); |
| | 1 | 118 | | } |
| | | 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 | | { |
| | 203 | 136 | | if (!message.IsEnabled) |
| | 201 | 137 | | return; |
| | | 138 | | |
| | 2 | 139 | | WriteCore(channel, DiagnosticLevel.Warning, message.Message, ResolveSource(source, method, filePath)); |
| | 2 | 140 | | } |
| | | 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 | | { |
| | 3 | 158 | | if (!message.IsEnabled) |
| | 2 | 159 | | return; |
| | | 160 | | |
| | 1 | 161 | | WriteCore(channel, DiagnosticLevel.Error, message.Message, ResolveSource(source, method, filePath)); |
| | 1 | 162 | | } |
| | | 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 | | { |
| | 4 | 182 | | if (!message.IsEnabled) |
| | 2 | 183 | | return; |
| | | 184 | | |
| | 2 | 185 | | WriteCore(channel, level, message.Message, ResolveSource(source, method, filePath)); |
| | 2 | 186 | | } |
| | | 187 | | |
| | | 188 | | private static void WriteCore( |
| | | 189 | | DiagnosticChannel channel, |
| | | 190 | | DiagnosticLevel level, |
| | | 191 | | DiagnosticInterpolatedStringHandler message, |
| | | 192 | | string source) |
| | | 193 | | { |
| | 7 | 194 | | ChronicleGuard.ThrowIfNull(channel, nameof(channel)); |
| | 6 | 195 | | channel.Write(level, message, source); |
| | 6 | 196 | | } |
| | | 197 | | |
| | | 198 | | private static string ResolveSource(string source, string method, string filePath) |
| | | 199 | | { |
| | 15 | 200 | | if (!string.IsNullOrEmpty(source)) |
| | 2 | 201 | | return source; |
| | | 202 | | |
| | 13 | 203 | | string className = Path.GetFileNameWithoutExtension(filePath); |
| | 13 | 204 | | if (string.IsNullOrEmpty(className)) |
| | 1 | 205 | | return method; |
| | | 206 | | |
| | 12 | 207 | | if (string.IsNullOrEmpty(method)) |
| | 1 | 208 | | return className; |
| | | 209 | | |
| | 11 | 210 | | return $"{className}.{method}"; |
| | | 211 | | } |
| | | 212 | | } |