2

I have the following log4net statement in a c# application:

log.Info(CultureInfo.InvariantCulture, m => m(notice));

with the string contents of:

notice = "Checking: 645: Bp $B!!:{4V7r;K Bp $B$D$^$M$5$S (B <xxx@xxxxxx. Co. Jp> (B <xxxxx@xxxxxxx.Com>)"

causing this exception:

[Common.Logging.Factory.AbstractLogger+FormatMessageCallbackFormattedMessage]System.FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list. at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args) at System.String.Format(IFormatProvider provider, String format, Object[] args) at Common.Logging.Factory.AbstractLogger.FormatMessageCallbackFormattedMessage.FormatMessage(String format, Object[] args)

If you notice in the string (which, in this case, is a totally piece of garbage) there is a single bracket "{". I'm fairly certain that this is causing the exception. What can I do to avoid this? Escape the string somehow?

It's a fairly harmless exception, except that it shows up in the log file and is distracting.

croxy
  • 4,082
  • 9
  • 28
  • 46
James John McGuire 'Jahmic'
  • 11,728
  • 11
  • 67
  • 78
  • 1
    I think you would be better tagging this as Common.Logging – sgmoore Jul 29 '13 at 11:58
  • what log4net are you using? `log.Info(CultureInfo.InvariantCulture, m => m(notice));` doesnt even compile as the 2nd argument to `log.Info` is an `Exception` not an Expression – wal Jul 29 '13 at 12:31
  • 1
    You can escape the `{` and `}` for use in `String.Format` by doubling them. So `{{` and `}}`. Or don't use String.Format if you don't want to. – Hans Kesting Jul 29 '13 at 14:20
  • @sqmoore, you are quite correct, 'common.logging', which would clarify some of the confusion of the other comments. – James John McGuire 'Jahmic' Jul 29 '13 at 17:10
  • @HansKesting - The { bracket is from the string data, I'm trying to process and I would like a more ideal option than massaging the string before output. Also, I'm not using string.Format myself. – James John McGuire 'Jahmic' Jul 29 '13 at 17:14

2 Answers2

2

It ends up that the Common.Logging log function uses the string.Format functions regardless of whether they are needed or not. So, as @HansKesting mentioned in comments, escaping any unintended brackets (braces) will be needed. So, when logging data that I suspect my have this problem, I changed the code to:

notice = notice.Replace("{", "{{");
log.Info(CultureInfo.InvariantCulture, m => m(notice));

Hopes this helps others.

James John McGuire 'Jahmic'
  • 11,728
  • 11
  • 67
  • 78
0

You are using a self writen extention which accepts a Action as log argument. Your method m(string) causes an error. You should check the source code of your method m. Log4net it self will never cause any errors because it is designed to fail silent. If the error is caused by log4net you would have find a critical bug.

The reason log4net does not accept a Action<string> as argument is that it can have side affects like this.

Peter
  • 27,590
  • 8
  • 64
  • 84