/// <summary> /// Expand <paramref name="exception"/> and inner exceptions to string. /// </summary> /// <param name="exception"></param> /// <param name="format"></param> /// <returns></returns> public static string ExpandToString(this Exception exception, string format = null) { if (format.IsNullOrWhiteSpace()) { format = new [] { "{0}: {1}", "Stack trace:", "{2}", }.Join(Environment.NewLine); } StringBuilder builder = exception .Aggregate(new StringBuilder(), (sb, e) => sb.AppendFormat(format, e.GetType().FullName, e.Message, e.StackTrace)); return(builder.ToString()); }