< Summary - Kestrun — Combined Coverage

Information
Class: Kestrun.Logging.LoggerExtensions
Assembly: Kestrun
File(s): /home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Logging/LoggerExtensions.cs
Tag: Kestrun/Kestrun@0d738bf294e6281b936d031e1979d928007495ff
Line coverage
91%
Covered lines: 22
Uncovered lines: 2
Coverable lines: 24
Total lines: 97
Line coverage: 91.6%
Branch coverage
83%
Covered branches: 10
Total branches: 12
Branch coverage: 83.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 08/26/2025 - 14:53:17 Line coverage: 94.4% (17/18) Branch coverage: 80% (8/10) Total lines: 71 Tag: Kestrun/Kestrun@78d1e497d8ba989d121b57aa39aa3c6b22de743109/06/2025 - 18:30:33 Line coverage: 94.4% (17/18) Branch coverage: 90% (9/10) Total lines: 71 Tag: Kestrun/Kestrun@aeddbedb8a96e9137aac94c2d5edd011b57ac87112/18/2025 - 21:41:58 Line coverage: 91.6% (22/24) Branch coverage: 83.3% (10/12) Total lines: 97 Tag: Kestrun/Kestrun@0d738bf294e6281b936d031e1979d928007495ff 08/26/2025 - 14:53:17 Line coverage: 94.4% (17/18) Branch coverage: 80% (8/10) Total lines: 71 Tag: Kestrun/Kestrun@78d1e497d8ba989d121b57aa39aa3c6b22de743109/06/2025 - 18:30:33 Line coverage: 94.4% (17/18) Branch coverage: 90% (9/10) Total lines: 71 Tag: Kestrun/Kestrun@aeddbedb8a96e9137aac94c2d5edd011b57ac87112/18/2025 - 21:41:58 Line coverage: 91.6% (22/24) Branch coverage: 83.3% (10/12) Total lines: 97 Tag: Kestrun/Kestrun@0d738bf294e6281b936d031e1979d928007495ff

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
DebugSanitized(...)100%22100%
DebugSanitized(...)50%2280%
ErrorSanitized(...)50%2280%
SanitizeObject(...)100%22100%
SanitizeString(...)100%44100%

File(s)

/home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Logging/LoggerExtensions.cs

#LineLine coverage
 1
 2using System.Text;
 3using Serilog.Events;
 4
 5namespace Kestrun.Logging;
 6
 7/// <summary>
 8/// Sanitized Serilog extensions to strip control chars (including CR/LF)
 9/// from any string property values before writing the log.
 10/// </summary>
 11public static class LoggerExtensions
 12{
 13    /// <summary>
 14    /// Writes a sanitized debug log event, removing control characters from string property values.
 15    /// </summary>
 16    /// <param name="log">The Serilog logger instance.</param>
 17    /// <param name="messageTemplate">The message template.</param>
 18    /// <param name="propertyValues">The property values for the message template.</param>
 19    public static void DebugSanitized(this Serilog.ILogger log, string messageTemplate, params object?[] propertyValues)
 20    {
 4421        if (!log.IsEnabled(LogEventLevel.Debug))
 22        {
 123            return;
 24        }
 25
 4326        var sanitized = propertyValues.Select(SanitizeObject).ToArray();
 4327        log.Debug(messageTemplate, sanitized);
 4328    }
 29
 30    /// <summary>
 31    /// Writes a sanitized debug log event with an exception, removing control characters from string property values.
 32    /// </summary>
 33    /// <param name="log">The Serilog logger instance.</param>
 34    /// <param name="exception">The exception to log.</param>
 35    /// <param name="messageTemplate">The message template.</param>
 36    /// <param name="propertyValues">The property values for the message template.</param>
 37    public static void DebugSanitized(this Serilog.ILogger log, Exception exception, string messageTemplate, params obje
 38    {
 139        if (!log.IsEnabled(LogEventLevel.Debug))
 40        {
 041            return;
 42        }
 43
 144        var sanitized = propertyValues.Select(SanitizeObject).ToArray();
 145        log.Debug(exception, messageTemplate, sanitized);
 146    }
 47
 48    /// <summary>
 49    /// Writes a sanitized error log event, removing control characters from string property values.
 50    /// </summary>
 51    /// <param name="log">The Serilog logger instance.</param>
 52    /// <param name="exception">The exception to log.</param>
 53    /// <param name="messageTemplate">The message template.</param>
 54    /// <param name="propertyValues">The property values for the message template.</param>
 55    public static void ErrorSanitized(this Serilog.ILogger log, Exception exception, string messageTemplate, params obje
 56    {
 157        if (!log.IsEnabled(LogEventLevel.Error))
 58        {
 059            return;
 60        }
 61
 162        var sanitized = propertyValues.Select(SanitizeObject).ToArray();
 163        log.Error(exception, messageTemplate, sanitized);
 164    }
 65
 66    /// <summary>
 67    /// Sanitizes an object by stripping control characters if it's a string.
 68    /// </summary>
 69    /// <param name="o">The object to sanitize.</param>
 70    /// <returns>The sanitized object.</returns>
 71    private static object? SanitizeObject(object? o) =>
 5072        o is string s
 5073            ? SanitizeString(s)
 5074            : o;
 75
 76    /// <summary>
 77    /// Sanitizes a string by removing control characters.
 78    /// Strip out all control characters (0x00–0x1F, 0x7F), including CR/LF
 79    /// </summary>
 80    /// <param name="input">The string to sanitize.</param>
 81    /// <returns>The sanitized string.</returns>
 82    private static string SanitizeString(string input)
 83    {
 384        var sb = new StringBuilder(input.Length).Append('"');
 10085        foreach (var c in input)
 86        {
 4787            if (char.IsControl(c))
 88            {
 89                continue;
 90            }
 91
 4192            _ = sb.Append(c);
 93        }
 394        _ = sb.Append('"');
 395        return sb.ToString();
 96    }
 97}