< 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@ca54e35c77799b76774b3805b6f075cdbc0c5fbe
Line coverage
76%
Covered lines: 26
Uncovered lines: 8
Coverable lines: 34
Total lines: 132
Line coverage: 76.4%
Branch coverage
68%
Covered branches: 11
Total branches: 16
Branch coverage: 68.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 09/08/2025 - 20:34:03 Line coverage: 94.4% (17/18) Branch coverage: 90% (9/10) Total lines: 71 Tag: Kestrun/Kestrun@3790ee5884494a7a2a829344a47743e0bf492e7212/18/2025 - 21:41:58 Line coverage: 91.6% (22/24) Branch coverage: 83.3% (10/12) Total lines: 97 Tag: Kestrun/Kestrun@0d738bf294e6281b936d031e1979d928007495ff01/17/2026 - 04:33:35 Line coverage: 64.7% (22/34) Branch coverage: 62.5% (10/16) Total lines: 132 Tag: Kestrun/Kestrun@aca34ea8d284564e2f9f6616dc937668dce926ba01/18/2026 - 06:40:41 Line coverage: 76.4% (26/34) Branch coverage: 68.7% (11/16) Total lines: 132 Tag: Kestrun/Kestrun@99e92690d0fd95f6f4896f3410d2c024350a9794 09/08/2025 - 20:34:03 Line coverage: 94.4% (17/18) Branch coverage: 90% (9/10) Total lines: 71 Tag: Kestrun/Kestrun@3790ee5884494a7a2a829344a47743e0bf492e7212/18/2025 - 21:41:58 Line coverage: 91.6% (22/24) Branch coverage: 83.3% (10/12) Total lines: 97 Tag: Kestrun/Kestrun@0d738bf294e6281b936d031e1979d928007495ff01/17/2026 - 04:33:35 Line coverage: 64.7% (22/34) Branch coverage: 62.5% (10/16) Total lines: 132 Tag: Kestrun/Kestrun@aca34ea8d284564e2f9f6616dc937668dce926ba01/18/2026 - 06:40:41 Line coverage: 76.4% (26/34) Branch coverage: 68.7% (11/16) Total lines: 132 Tag: Kestrun/Kestrun@99e92690d0fd95f6f4896f3410d2c024350a9794

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
DebugSanitized(...)100%22100%
DebugSanitized(...)50%2280%
ErrorSanitized(...)50%2280%
WarningSanitized(...)0%620%
WarningSanitized(...)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    {
 7121        if (!log.IsEnabled(LogEventLevel.Debug))
 22        {
 123            return;
 24        }
 25
 7026        var sanitized = propertyValues.Select(SanitizeObject).ToArray();
 7027        log.Debug(messageTemplate, sanitized);
 7028    }
 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    /// Writes a sanitized warning log event, removing control characters from string property values.
 68    /// </summary>
 69    /// <param name="log">The Serilog logger instance.</param>
 70    /// <param name="exception">The exception to log.</param>
 71    /// <param name="messageTemplate">The message template.</param>
 72    /// <param name="propertyValues">The property values for the message template.</param>
 73    public static void WarningSanitized(this Serilog.ILogger log, Exception exception, string messageTemplate, params ob
 74    {
 075        if (!log.IsEnabled(LogEventLevel.Warning))
 76        {
 077            return;
 78        }
 79
 080        var sanitized = propertyValues.Select(SanitizeObject).ToArray();
 081        log.Warning(exception, messageTemplate, sanitized);
 082    }
 83
 84    /// <summary>
 85    /// Writes a sanitized warning log event, removing control characters from string property values.
 86    /// </summary>
 87    /// <param name="log">The Serilog logger instance.</param>
 88    /// <param name="messageTemplate">The message template.</param>
 89    /// <param name="propertyValues">The property values for the message template.</param>
 90    public static void WarningSanitized(this Serilog.ILogger log, string messageTemplate, params object?[] propertyValue
 91    {
 692        if (!log.IsEnabled(LogEventLevel.Warning))
 93        {
 094            return;
 95        }
 96
 697        var sanitized = propertyValues.Select(SanitizeObject).ToArray();
 698        log.Warning(messageTemplate, sanitized);
 699    }
 100
 101    /// <summary>
 102    /// Sanitizes an object by stripping control characters if it's a string.
 103    /// </summary>
 104    /// <param name="o">The object to sanitize.</param>
 105    /// <returns>The sanitized object.</returns>
 106    private static object? SanitizeObject(object? o) =>
 137107        o is string s
 137108            ? SanitizeString(s)
 137109            : o;
 110
 111    /// <summary>
 112    /// Sanitizes a string by removing control characters.
 113    /// Strip out all control characters (0x00–0x1F, 0x7F), including CR/LF
 114    /// </summary>
 115    /// <param name="input">The string to sanitize.</param>
 116    /// <returns>The sanitized string.</returns>
 117    private static string SanitizeString(string input)
 118    {
 63119        var sb = new StringBuilder(input.Length).Append('"');
 1224120        foreach (var c in input)
 121        {
 549122            if (char.IsControl(c))
 123            {
 124                continue;
 125            }
 126
 543127            _ = sb.Append(c);
 128        }
 63129        _ = sb.Append('"');
 63130        return sb.ToString();
 131    }
 132}