private string FormatMessage(string format, object[] obj)
{
if (obj == null || obj.Length == 0) return format;
string msg;
try
{
msg = string.Format(System.Globalization.CultureInfo.InvariantCulture, format, obj);
}
catch (Exception e)
{
// Don't allow log message formatting to throw unhandled exceptions,
// because they would result in another log - and probably more exceptions.
// Instead, embed format, arguments and the exception in the resulting
// log message, so the user can retrieve all necessary information for
// fixing his log call.
msg = format + Environment.NewLine;
if (obj != null)
{
try
{
msg += obj.ToString(", ") + Environment.NewLine;
}
catch (Exception)
{
msg += "(Error in ToString call)" + Environment.NewLine;
}
}
msg += Log.Exception(e);
}
return msg;
}