Skip to content

Commit

Permalink
(GH-202)(log) Enhance formatting escaping
Browse files Browse the repository at this point in the history
- Escape curly braces only when there is a single curly brace - allows
idempotency
- Use Format calls on regular logging calls so that format escaped items
are properly logged
  • Loading branch information
ferventcoder committed Mar 29, 2015
1 parent b227b35 commit 8f5ee90
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/chocolatey/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,14 @@ public static string remove_surrounding_quotes(this string input)
return input;
}

private static Regex open_brace_regex = new Regex("(?<!{){(?!{)", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Compiled);
private static Regex close_brace_regex = new Regex("(?<!})}(?!})", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Compiled);

public static string escape_curly_braces(this string input)
{
if (string.IsNullOrWhiteSpace(input)) return string.Empty;

return input.Replace("{", "{{").Replace("}", "}}");
return open_brace_regex.Replace(close_brace_regex.Replace(input,"}}"),"{{");
}
}
}
11 changes: 5 additions & 6 deletions src/chocolatey/infrastructure/logging/Log4NetLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void Debug(string message, params object[] formatting)

public void Debug(Func<string> message)
{
if (_logger.IsDebugEnabled) _logger.Debug(decorate_message_with_audit_information(message.Invoke().escape_curly_braces()));
if (_logger.IsDebugEnabled) _logger.DebugFormat(decorate_message_with_audit_information(message.Invoke()).escape_curly_braces());
}

public void Info(string message, params object[] formatting)
Expand All @@ -53,8 +53,7 @@ public void Info(string message, params object[] formatting)

public void Info(Func<string> message)
{
var messageT = decorate_message_with_audit_information(message.Invoke().escape_curly_braces());
if (_logger.IsInfoEnabled) _logger.Info(decorate_message_with_audit_information(message.Invoke().escape_curly_braces()));
if (_logger.IsInfoEnabled) _logger.InfoFormat(decorate_message_with_audit_information(message.Invoke()).escape_curly_braces());
}

public void Warn(string message, params object[] formatting)
Expand All @@ -64,7 +63,7 @@ public void Warn(string message, params object[] formatting)

public void Warn(Func<string> message)
{
if (_logger.IsWarnEnabled) _logger.Warn(decorate_message_with_audit_information(message.Invoke().escape_curly_braces()));
if (_logger.IsWarnEnabled) _logger.WarnFormat(decorate_message_with_audit_information(message.Invoke()).escape_curly_braces());
}

public void Error(string message, params object[] formatting)
Expand All @@ -76,7 +75,7 @@ public void Error(string message, params object[] formatting)
public void Error(Func<string> message)
{
// don't need to check for enabled at this level
_logger.Error(decorate_message_with_audit_information(message.Invoke().escape_curly_braces()));
_logger.ErrorFormat(decorate_message_with_audit_information(message.Invoke()).escape_curly_braces());
}

public void Fatal(string message, params object[] formatting)
Expand All @@ -88,7 +87,7 @@ public void Fatal(string message, params object[] formatting)
public void Fatal(Func<string> message)
{
// don't need to check for enabled at this level
_logger.Fatal(decorate_message_with_audit_information(message.Invoke().escape_curly_braces()));
_logger.FatalFormat(decorate_message_with_audit_information(message.Invoke()).escape_curly_braces());
}

public string decorate_message_with_audit_information(string message)
Expand Down

0 comments on commit 8f5ee90

Please sign in to comment.