| | | 1 | | |
| | | 2 | | using System.Text; |
| | | 3 | | using Serilog.Events; |
| | | 4 | | |
| | | 5 | | |
| | | 6 | | namespace 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> |
| | | 12 | | public 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 | | { |
| | 34 | 22 | | if (!log.IsEnabled(LogEventLevel.Debug)) |
| | | 23 | | { |
| | 1 | 24 | | return; |
| | | 25 | | } |
| | | 26 | | |
| | 33 | 27 | | var sanitized = propertyValues.Select(SanitizeObject).ToArray(); |
| | 33 | 28 | | log.Debug(messageTemplate, sanitized); |
| | 33 | 29 | | } |
| | | 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 | | { |
| | 1 | 41 | | if (!log.IsEnabled(LogEventLevel.Debug)) |
| | | 42 | | { |
| | 0 | 43 | | return; |
| | | 44 | | } |
| | | 45 | | |
| | 1 | 46 | | var sanitized = propertyValues.Select(SanitizeObject).ToArray(); |
| | 1 | 47 | | log.Debug(exception, messageTemplate, sanitized); |
| | 1 | 48 | | } |
| | | 49 | | |
| | | 50 | | // Helper: sanitize only string args |
| | | 51 | | private static object? SanitizeObject(object? o) => |
| | 39 | 52 | | o is string s |
| | 39 | 53 | | ? SanitizeString(s) |
| | 39 | 54 | | : o; |
| | | 55 | | |
| | | 56 | | // Strip out all control characters (0x00–0x1F, 0x7F), including CR/LF |
| | | 57 | | private static string SanitizeString(string input) |
| | | 58 | | { |
| | 3 | 59 | | var sb = new StringBuilder(input.Length); |
| | 100 | 60 | | foreach (var c in input) |
| | | 61 | | { |
| | 47 | 62 | | if (char.IsControl(c)) |
| | | 63 | | { |
| | | 64 | | continue; |
| | | 65 | | } |
| | | 66 | | |
| | 41 | 67 | | _ = sb.Append(c); |
| | | 68 | | } |
| | 3 | 69 | | return sb.ToString(); |
| | | 70 | | } |
| | | 71 | | } |