Jef Claes

On software and life

20 Jan 2010

Log4Net: log.Debug(String.Format()) versus log.DebugFormat()

Log4net is one of the most popular opensource logging frameworks available in the .NET world. I’ve been using this framework for over a year now, and today I discovered something new.

I often use string.Format() to format my log messages. Earlier this morning I made a typo formatting my message and an Exception was thrown in the beginning of my method which caused the application flow to break. You can avoid this by using the DebugFormat() method. If you mistype here, no exception will be thrown, but a WARN message will be logged.

Example using String.Format

log.Debug(string.Format("{0}{1}", "ABC_1"));

As expected this throws a System.FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

Example using log.DebugFormat

log.DebugFormat("{0}{1}", "ABC_1");

Using DebugFormat() the logger logs a WARN message, but it doesn’t break the application flow.

WARN StringFormat: Exception while rendering format [{0} {1}.]

So to avoid logging breaking your application, you should use the DebugFormat(), WarningFormat(), InfoFormat(), ErrorFormat() methods instead of String.Format() to format your log message.