fluffynuts commented on code in PR #325:
URL: https://github.com/apache/logging-log4net/pull/325#discussion_r4101482866
##########
src/log4net/Appender/LocalSyslogAppender.cs:
##########
@@ -372,9 +371,18 @@ protected override void Append(LoggingEvent loggingEvent)
message = EscapeNewLines(message);
}
- NativeMethods.syslog(priority, "%s", message);
+ NativeMethods.syslog(priority, EscapePercent(message));
}
+ /// <summary>
+ /// Doubles every <c>%</c>, so that the record libc takes as a printf format
string renders as
+ /// the text the layout produced.
+ /// </summary>
+ /// <param name="message">The rendered message.</param>
+ /// <returns>The message with every percent sign escaped.</returns>
+ private static string EscapePercent(string message)
+ => message.IndexOf('%') < 0 ? message : message.Replace("%", "%%");
Review Comment:
very interesting - i expected that a straight-up replace would be
sufficient, but I benchmarked a straight replace vs index-then-replace and it's
significantly faster because (looking at the source) any .Replace generates a
new string via a builder; one would have thought such an obvious optimisation
would live in the runtime 😅
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]