< 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
15%
Covered lines: 6
Uncovered lines: 34
Coverable lines: 40
Total lines: 118
Line coverage: 15%
Branch coverage
18%
Covered branches: 3
Total branches: 16
Branch coverage: 18.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
LogInformationSanitized(...)0%620%
LogWarningSanitized(...)50%3240%
LogErrorSanitized(...)0%620%
LogErrorSanitized(...)50%3240%
LogDebugSanitized(...)50%3240%
LogTraceSanitized(...)0%620%
LogCriticalSanitized(...)0%620%
LogCriticalSanitized(...)0%620%

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    {
 018        if (!logger.IsEnabled(LogLevel.Information))
 019            return;
 20
 021        var sanitizedArgs = args.Select(arg => Utilities.LogSanitizer.SanitizeObject(arg)).ToArray();
 022        logger.LogInformation(message, sanitizedArgs);
 023    }
 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    {
 431        if (!logger.IsEnabled(LogLevel.Warning))
 432            return;
 33
 034        var sanitizedArgs = args.Select(arg => Utilities.LogSanitizer.SanitizeObject(arg)).ToArray();
 035        logger.LogWarning(message, sanitizedArgs);
 036    }
 37
 38    /// <summary>
 39    /// Logs an error message with sanitized arguments.
 40    /// Use this when logging user-provided data.
 41    /// </summary>
 42    public static void LogErrorSanitized(this ILogger logger, string message, params object?[] args)
 43    {
 044        if (!logger.IsEnabled(LogLevel.Error))
 045            return;
 46
 047        var sanitizedArgs = args.Select(arg => Utilities.LogSanitizer.SanitizeObject(arg)).ToArray();
 48
 049        logger.LogError(message, sanitizedArgs);
 050    }
 51
 52    /// <summary>
 53    /// Logs an error message with exception and sanitized arguments.
 54    /// Use this when logging user-provided data along with an exception.
 55    /// </summary>
 56    public static void LogErrorSanitized(this ILogger logger, Exception exception, string message, params object?[] args
 57    {
 258        if (!logger.IsEnabled(LogLevel.Error))
 259            return;
 60
 061        var sanitizedArgs = args.Select(arg => Utilities.LogSanitizer.SanitizeObject(arg)).ToArray();
 062        logger.LogError(exception, message, sanitizedArgs);
 063    }
 64
 65    /// <summary>
 66    /// Logs a debug message with sanitized arguments.
 67    /// Use this when logging user-provided data.
 68    /// </summary>
 69    public static void LogDebugSanitized(this ILogger logger, string message, params object?[] args)
 70    {
 2771        if (!logger.IsEnabled(LogLevel.Debug))
 2772            return;
 73
 074        var sanitizedArgs = args.Select(arg => Utilities.LogSanitizer.SanitizeObject(arg)).ToArray();
 075        logger.LogDebug(message, sanitizedArgs);
 076    }
 77
 78    /// <summary>
 79    /// Logs a trace message with sanitized arguments.
 80    /// Use this when logging user-provided data.
 81    /// </summary>
 82    public static void LogTraceSanitized(this ILogger logger, string message, params object?[] args)
 83    {
 084        if (!logger.IsEnabled(LogLevel.Trace))
 085            return;
 86
 087        var sanitizedArgs = args.Select(arg => Utilities.LogSanitizer.SanitizeObject(arg)).ToArray();
 088        logger.LogTrace(message, sanitizedArgs);
 089    }
 90
 91    /// <summary>
 92    /// Logs a critical message with sanitized arguments.
 93    /// Use this when logging user-provided data.
 94    /// </summary>
 95    public static void LogCriticalSanitized(this ILogger logger, string message, params object?[] args)
 96    {
 097        if (!logger.IsEnabled(LogLevel.Critical))
 098            return;
 99
 0100        var sanitizedArgs = args.Select(arg => Utilities.LogSanitizer.SanitizeObject(arg)).ToArray();
 0101        logger.LogCritical(message, sanitizedArgs);
 0102    }
 103
 104    /// <summary>
 105    /// Logs a critical message with exception and sanitized arguments.
 106    /// Use this when logging user-provided data along with an exception.
 107    /// </summary>
 108    public static void LogCriticalSanitized(this ILogger logger, Exception exception, string message, params object?[] a
 109    {
 0110        if (!logger.IsEnabled(LogLevel.Critical))
 0111            return;
 112
 0113        var sanitizedArgs = args.Select(arg => Utilities.LogSanitizer.SanitizeObject(arg)).ToArray();
 0114        logger.LogCritical(exception, message, sanitizedArgs);
 0115    }
 116
 117#pragma warning restore CA2254
 118}