< Summary

Information
Class: Chronicis.Shared.Extensions.LoggerExtensions
Assembly: Chronicis.Shared
File(s): /home/runner/work/chronicis/chronicis/src/Chronicis.Shared/Extensions/LoggerExtensions.cs
Line coverage
100%
Covered lines: 50
Uncovered lines: 0
Coverable lines: 50
Total lines: 144
Line coverage: 100%
Branch coverage
100%
Covered branches: 20
Total branches: 20
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
LogInformationSanitized(...)100%22100%
LogWarningSanitized(...)100%22100%
LogWarningSanitized(...)100%22100%
LogErrorSanitized(...)100%22100%
LogErrorSanitized(...)100%22100%
LogDebugSanitized(...)100%22100%
LogTraceSanitized(...)100%22100%
LogTraceSanitized(...)100%22100%
LogCriticalSanitized(...)100%22100%
LogCriticalSanitized(...)100%22100%

File(s)

/home/runner/work/chronicis/chronicis/src/Chronicis.Shared/Extensions/LoggerExtensions.cs

#LineLine coverage
 1using Microsoft.Extensions.Logging;
 2
 3namespace Chronicis.Shared.Extensions;
 4
 5/// <summary>
 6/// Extension methods for ILogger that automatically sanitize user input before logging.
 7/// These methods should be used whenever logging data that originates from user input.
 8/// </summary>
 9public static class LoggerExtensions
 10{
 11#pragma warning disable CA2254
 12    /// <summary>
 13    /// Logs an informational message with sanitized arguments.
 14    /// Use this when logging user-provided data.
 15    /// </summary>
 16    public static void LogInformationSanitized(this ILogger logger, string message, params object?[] args)
 17    {
 1018        if (!logger.IsEnabled(LogLevel.Information))
 219            return;
 20
 821        var sanitizedArgs = args.Select(arg => Utilities.LogSanitizer.SanitizeObject(arg)).ToArray();
 822        logger.LogInformation(message, sanitizedArgs);
 823    }
 24
 25    /// <summary>
 26    /// Logs a warning message with sanitized arguments.
 27    /// Use this when logging user-provided data.
 28    /// </summary>
 29    public static void LogWarningSanitized(this ILogger logger, string message, params object?[] args)
 30    {
 531        if (!logger.IsEnabled(LogLevel.Warning))
 232            return;
 33
 334        var sanitizedArgs = args.Select(arg => Utilities.LogSanitizer.SanitizeObject(arg)).ToArray();
 335        logger.LogWarning(message, sanitizedArgs);
 336    }
 37
 38    /// <summary>
 39    /// Logs a warning message with exception and sanitized arguments.
 40    /// Use this when logging user-provided data along with an exception.
 41    /// </summary>
 42    public static void LogWarningSanitized(this ILogger logger, Exception exception, string message, params object?[] ar
 43    {
 244        if (!logger.IsEnabled(LogLevel.Warning))
 145            return;
 46
 147        var sanitizedArgs = args.Select(arg => Utilities.LogSanitizer.SanitizeObject(arg)).ToArray();
 148        logger.LogWarning(exception, message, sanitizedArgs);
 149    }
 50
 51    /// <summary>
 52    /// Logs an error message with sanitized arguments.
 53    /// Use this when logging user-provided data.
 54    /// </summary>
 55    public static void LogErrorSanitized(this ILogger logger, string message, params object?[] args)
 56    {
 557        if (!logger.IsEnabled(LogLevel.Error))
 258            return;
 59
 360        var sanitizedArgs = args.Select(arg => Utilities.LogSanitizer.SanitizeObject(arg)).ToArray();
 61
 362        logger.LogError(message, sanitizedArgs);
 363    }
 64
 65    /// <summary>
 66    /// Logs an error message with exception and sanitized arguments.
 67    /// Use this when logging user-provided data along with an exception.
 68    /// </summary>
 69    public static void LogErrorSanitized(this ILogger logger, Exception exception, string message, params object?[] args
 70    {
 371        if (!logger.IsEnabled(LogLevel.Error))
 172            return;
 73
 274        var sanitizedArgs = args.Select(arg => Utilities.LogSanitizer.SanitizeObject(arg)).ToArray();
 275        logger.LogError(exception, message, sanitizedArgs);
 276    }
 77
 78    /// <summary>
 79    /// Logs a debug message with sanitized arguments.
 80    /// Use this when logging user-provided data.
 81    /// </summary>
 82    public static void LogDebugSanitized(this ILogger logger, string message, params object?[] args)
 83    {
 584        if (!logger.IsEnabled(LogLevel.Debug))
 285            return;
 86
 387        var sanitizedArgs = args.Select(arg => Utilities.LogSanitizer.SanitizeObject(arg)).ToArray();
 388        logger.LogDebug(message, sanitizedArgs);
 389    }
 90
 91    /// <summary>
 92    /// Logs a trace message with sanitized arguments.
 93    /// Use this when logging user-provided data.
 94    /// </summary>
 95    public static void LogTraceSanitized(this ILogger logger, string message, params object?[] args)
 96    {
 597        if (!logger.IsEnabled(LogLevel.Trace))
 298            return;
 99
 3100        var sanitizedArgs = args.Select(arg => Utilities.LogSanitizer.SanitizeObject(arg)).ToArray();
 3101        logger.LogTrace(message, sanitizedArgs);
 3102    }
 103
 104    /// <summary>
 105    /// Logs a trace message with exception and sanitized arguments.
 106    /// Use this when logging user-provided data along with an exception.
 107    /// </summary>
 108    public static void LogTraceSanitized(this ILogger logger, Exception exception, string message, params object?[] args
 109    {
 2110        if (!logger.IsEnabled(LogLevel.Trace))
 1111            return;
 112
 1113        var sanitizedArgs = args.Select(arg => Utilities.LogSanitizer.SanitizeObject(arg)).ToArray();
 1114        logger.LogTrace(exception, message, sanitizedArgs);
 1115    }
 116
 117    /// <summary>
 118    /// Logs a critical message with sanitized arguments.
 119    /// Use this when logging user-provided data.
 120    /// </summary>
 121    public static void LogCriticalSanitized(this ILogger logger, string message, params object?[] args)
 122    {
 5123        if (!logger.IsEnabled(LogLevel.Critical))
 2124            return;
 125
 3126        var sanitizedArgs = args.Select(arg => Utilities.LogSanitizer.SanitizeObject(arg)).ToArray();
 3127        logger.LogCritical(message, sanitizedArgs);
 3128    }
 129
 130    /// <summary>
 131    /// Logs a critical message with exception and sanitized arguments.
 132    /// Use this when logging user-provided data along with an exception.
 133    /// </summary>
 134    public static void LogCriticalSanitized(this ILogger logger, Exception exception, string message, params object?[] a
 135    {
 2136        if (!logger.IsEnabled(LogLevel.Critical))
 1137            return;
 138
 1139        var sanitizedArgs = args.Select(arg => Utilities.LogSanitizer.SanitizeObject(arg)).ToArray();
 1140        logger.LogCritical(exception, message, sanitizedArgs);
 1141    }
 142
 143#pragma warning restore CA2254
 144}