| | | 1 | | using Microsoft.Extensions.Logging; |
| | | 2 | | |
| | | 3 | | namespace 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> |
| | | 9 | | public 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 | | { |
| | 0 | 18 | | if (!logger.IsEnabled(LogLevel.Information)) |
| | 0 | 19 | | return; |
| | | 20 | | |
| | 0 | 21 | | var sanitizedArgs = args.Select(arg => Utilities.LogSanitizer.SanitizeObject(arg)).ToArray(); |
| | 0 | 22 | | logger.LogInformation(message, sanitizedArgs); |
| | 0 | 23 | | } |
| | | 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 | | { |
| | 4 | 31 | | if (!logger.IsEnabled(LogLevel.Warning)) |
| | 4 | 32 | | return; |
| | | 33 | | |
| | 0 | 34 | | var sanitizedArgs = args.Select(arg => Utilities.LogSanitizer.SanitizeObject(arg)).ToArray(); |
| | 0 | 35 | | logger.LogWarning(message, sanitizedArgs); |
| | 0 | 36 | | } |
| | | 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 | | { |
| | 0 | 44 | | if (!logger.IsEnabled(LogLevel.Error)) |
| | 0 | 45 | | return; |
| | | 46 | | |
| | 0 | 47 | | var sanitizedArgs = args.Select(arg => Utilities.LogSanitizer.SanitizeObject(arg)).ToArray(); |
| | | 48 | | |
| | 0 | 49 | | logger.LogError(message, sanitizedArgs); |
| | 0 | 50 | | } |
| | | 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 | | { |
| | 2 | 58 | | if (!logger.IsEnabled(LogLevel.Error)) |
| | 2 | 59 | | return; |
| | | 60 | | |
| | 0 | 61 | | var sanitizedArgs = args.Select(arg => Utilities.LogSanitizer.SanitizeObject(arg)).ToArray(); |
| | 0 | 62 | | logger.LogError(exception, message, sanitizedArgs); |
| | 0 | 63 | | } |
| | | 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 | | { |
| | 27 | 71 | | if (!logger.IsEnabled(LogLevel.Debug)) |
| | 27 | 72 | | return; |
| | | 73 | | |
| | 0 | 74 | | var sanitizedArgs = args.Select(arg => Utilities.LogSanitizer.SanitizeObject(arg)).ToArray(); |
| | 0 | 75 | | logger.LogDebug(message, sanitizedArgs); |
| | 0 | 76 | | } |
| | | 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 | | { |
| | 0 | 84 | | if (!logger.IsEnabled(LogLevel.Trace)) |
| | 0 | 85 | | return; |
| | | 86 | | |
| | 0 | 87 | | var sanitizedArgs = args.Select(arg => Utilities.LogSanitizer.SanitizeObject(arg)).ToArray(); |
| | 0 | 88 | | logger.LogTrace(message, sanitizedArgs); |
| | 0 | 89 | | } |
| | | 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 | | { |
| | 0 | 97 | | if (!logger.IsEnabled(LogLevel.Critical)) |
| | 0 | 98 | | return; |
| | | 99 | | |
| | 0 | 100 | | var sanitizedArgs = args.Select(arg => Utilities.LogSanitizer.SanitizeObject(arg)).ToArray(); |
| | 0 | 101 | | logger.LogCritical(message, sanitizedArgs); |
| | 0 | 102 | | } |
| | | 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 | | { |
| | 0 | 110 | | if (!logger.IsEnabled(LogLevel.Critical)) |
| | 0 | 111 | | return; |
| | | 112 | | |
| | 0 | 113 | | var sanitizedArgs = args.Select(arg => Utilities.LogSanitizer.SanitizeObject(arg)).ToArray(); |
| | 0 | 114 | | logger.LogCritical(exception, message, sanitizedArgs); |
| | 0 | 115 | | } |
| | | 116 | | |
| | | 117 | | #pragma warning restore CA2254 |
| | | 118 | | } |