< 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@9d3a582b2d63930269564a7591aa77ef297cadeb
Line coverage
94%
Covered lines: 17
Uncovered lines: 1
Coverable lines: 18
Total lines: 71
Line coverage: 94.4%
Branch coverage
90%
Covered branches: 9
Total branches: 10
Branch coverage: 90%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
DebugSanitized(...)100%22100%
DebugSanitized(...)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
 5
 6namespace Kestrun.Logging;
 7
 8/// <summary>
 9/// Sanitized Serilog extensions to strip control chars (including CR/LF)
 10/// from any string property values before writing the log.
 11/// </summary>
 12public static class LoggerExtensions
 13{
 14    /// <summary>
 15    /// Writes a sanitized debug log event, removing control characters from string property values.
 16    /// </summary>
 17    /// <param name="log">The Serilog logger instance.</param>
 18    /// <param name="messageTemplate">The message template.</param>
 19    /// <param name="propertyValues">The property values for the message template.</param>
 20    public static void DebugSanitized(this Serilog.ILogger log, string messageTemplate, params object?[] propertyValues)
 21    {
 2222        if (!log.IsEnabled(LogEventLevel.Debug))
 23        {
 124            return;
 25        }
 26
 2127        var sanitized = propertyValues.Select(SanitizeObject).ToArray();
 2128        log.Debug(messageTemplate, sanitized);
 2129    }
 30
 31
 32    /// <summary>
 33    /// Writes a sanitized debug log event with an exception, removing control characters from string property values.
 34    /// </summary>
 35    /// <param name="log">The Serilog logger instance.</param>
 36    /// <param name="exception">The exception to log.</param>
 37    /// <param name="messageTemplate">The message template.</param>
 38    /// <param name="propertyValues">The property values for the message template.</param>
 39    public static void DebugSanitized(this Serilog.ILogger log, Exception exception, string messageTemplate, params obje
 40    {
 141        if (!log.IsEnabled(LogEventLevel.Debug))
 42        {
 043            return;
 44        }
 45
 146        var sanitized = propertyValues.Select(SanitizeObject).ToArray();
 147        log.Debug(exception, messageTemplate, sanitized);
 148    }
 49
 50    // Helper: sanitize only string args
 51    private static object? SanitizeObject(object? o) =>
 2252        o is string s
 2253            ? SanitizeString(s)
 2254            : o;
 55
 56    // Strip out all control characters (0x00–0x1F, 0x7F), including CR/LF
 57    private static string SanitizeString(string input)
 58    {
 259        var sb = new StringBuilder(input.Length);
 4260        foreach (var c in input)
 61        {
 1962            if (char.IsControl(c))
 63            {
 64                continue;
 65            }
 66
 1367            _ = sb.Append(c);
 68        }
 269        return sb.ToString();
 70    }
 71}