| | | 1 | | |
| | | 2 | | using System.Text; |
| | | 3 | | using Serilog.Events; |
| | | 4 | | |
| | | 5 | | namespace 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> |
| | | 11 | | public 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 | | { |
| | 44 | 21 | | if (!log.IsEnabled(LogEventLevel.Debug)) |
| | | 22 | | { |
| | 1 | 23 | | return; |
| | | 24 | | } |
| | | 25 | | |
| | 43 | 26 | | var sanitized = propertyValues.Select(SanitizeObject).ToArray(); |
| | 43 | 27 | | log.Debug(messageTemplate, sanitized); |
| | 43 | 28 | | } |
| | | 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 | | { |
| | 1 | 39 | | if (!log.IsEnabled(LogEventLevel.Debug)) |
| | | 40 | | { |
| | 0 | 41 | | return; |
| | | 42 | | } |
| | | 43 | | |
| | 1 | 44 | | var sanitized = propertyValues.Select(SanitizeObject).ToArray(); |
| | 1 | 45 | | log.Debug(exception, messageTemplate, sanitized); |
| | 1 | 46 | | } |
| | | 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 | | { |
| | 1 | 57 | | if (!log.IsEnabled(LogEventLevel.Error)) |
| | | 58 | | { |
| | 0 | 59 | | return; |
| | | 60 | | } |
| | | 61 | | |
| | 1 | 62 | | var sanitized = propertyValues.Select(SanitizeObject).ToArray(); |
| | 1 | 63 | | log.Error(exception, messageTemplate, sanitized); |
| | 1 | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Sanitizes an object by stripping control characters if it's a string. |
| | | 68 | | /// </summary> |
| | | 69 | | /// <param name="o">The object to sanitize.</param> |
| | | 70 | | /// <returns>The sanitized object.</returns> |
| | | 71 | | private static object? SanitizeObject(object? o) => |
| | 50 | 72 | | o is string s |
| | 50 | 73 | | ? SanitizeString(s) |
| | 50 | 74 | | : o; |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Sanitizes a string by removing control characters. |
| | | 78 | | /// Strip out all control characters (0x00–0x1F, 0x7F), including CR/LF |
| | | 79 | | /// </summary> |
| | | 80 | | /// <param name="input">The string to sanitize.</param> |
| | | 81 | | /// <returns>The sanitized string.</returns> |
| | | 82 | | private static string SanitizeString(string input) |
| | | 83 | | { |
| | 3 | 84 | | var sb = new StringBuilder(input.Length).Append('"'); |
| | 100 | 85 | | foreach (var c in input) |
| | | 86 | | { |
| | 47 | 87 | | if (char.IsControl(c)) |
| | | 88 | | { |
| | | 89 | | continue; |
| | | 90 | | } |
| | | 91 | | |
| | 41 | 92 | | _ = sb.Append(c); |
| | | 93 | | } |
| | 3 | 94 | | _ = sb.Append('"'); |
| | 3 | 95 | | return sb.ToString(); |
| | | 96 | | } |
| | | 97 | | } |