public XmlSerializableException(System.Exception ex) { StackTrace = ex.StackTrace; Source = ex.Source; Message = ex.Message; HResult = ex.HResult; HelpLink = ex.HelpLink; var prop = ex.GetType().GetProperty("Statement"); if (prop != null) { var statement = prop.GetValue(ex); if (statement != null) { SqlStatement = statement.GetType().GetProperty("SQL")?.GetValue(statement).ToString(); } } if (ex.InnerException != null) { InnerException = new XmlSerializableException(ex.InnerException); } }
public static string SerializeToXml(this Exception ex) { try { XmlSerializableException toSerialize = new XmlSerializableException(ex); XmlSerializer xmlSerializer = new XmlSerializer(toSerialize.GetType()); using (StringWriter textWriter = new StringWriter()) { xmlSerializer.Serialize(textWriter, toSerialize); return(textWriter.ToString()); } } catch (Exception) { return("XML serialization failed!"); } }