private static void logException(Exception ex, string filePath)
{
string timestamp = DateTime.Now.ToString();
StringBuilder sb = new StringBuilder();
int i = 1;
while (ex != null)
{
if (i == 1)
{
sb.AppendLine(String.Format("[{1}]:{0}Message: {2}{0}Source: {3}{0}StackTrace: {4}",
Environment.NewLine, timestamp, ex.Message, ex.Source, ex.StackTrace));
}
else
{
sb.AppendLine(String.Format("Inner exception:"));
sb.AppendLine(String.Format("{0}Message: {2}{1}{0}Source: {3}{1}{0}StackTrace: {4}",
new String(\'\\t\', i), Environment.NewLine, ex.Message, ex.Source, ex.StackTrace));
}
ex = ex.InnerException;
i++;
}
using (StreamWriter sw = new StreamWriter(filePath, true))
{
sw.WriteLine(sb.ToString());
}
}