// -------------------------------------------------------------------------------------------------------------------------- public ExceptionDetail(Exception ex, string comment_) { Type = ex.GetType().Name; Message = ex.Message; Comment = comment_; string[] trace = (ex.StackTrace != null) ? ex.StackTrace.Split(new[] { Environment.NewLine }, StringSplitOptions.None) : new string[] { }; _StackTrace = new ReadOnlyCollection <string>(trace); // The inner exception on an aggregate is also the first item in its list of causes, so we skip it. if (ex.InnerException != null && !(ex is AggregateException)) { InnerException = new ExceptionDetail(ex.InnerException); } var agg = ex as AggregateException; if (agg != null) { InnerExceptions = new List <ExceptionDetail>(); foreach (var e in agg.InnerExceptions) { InnerExceptions.Add(new ExceptionDetail(e)); } } }
// -------------------------------------------------------------------------------------------------------------------------- private static XDocument GetXMLInner(ExceptionDetail detail) { XElement xml = new XElement("ExceptionReport", new XAttribute("version", "2"), detail.ToXMLInner()); XDocument res = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), xml); return(res); }
public static string SaveExceptionData(Exception ex, string exDir, bool noThrow = true) #endif { try { #if NETFX_CORE StorageFolder f = await FileTools.CreateDirectory(exDir); #else FileTools.CreateDirectory(exDir); #endif #if NETFX_CORE ExceptionDetail ed = new ExceptionDetail(ex); string exPath = FileTools.GetSequentialFileName(f, "Exception", ".xml"); StorageFile target = await FileTools.CreateFile(exPath, true); await FileTools.WriteAllText(target, ed.ToString()); #else ExceptionDetail ed = new ExceptionDetail(ex); string exPath = FileTools.GetSequentialFileName(exDir, "Exception", ".xml"); ed.ToXML().Save(exPath); #endif return(exPath); } catch (Exception) { if (!noThrow) { throw; } // EMPTY: This can't fail! return(null); } }
// -------------------------------------------------------------------------------------------------------------------------- public static ExceptionDetail FromXML(XDocument doc) { XElement root = doc.Root; if (root.Name != "ExceptionReport") { throw new InvalidOperationException("Invalid root node!"); } var vAttr = root.Attribute("version"); int version = vAttr == null ? 1 : int.Parse(vAttr.Value); ExceptionDetail detail = FromXMLChild(root.Element("Exception"), version); return(detail); }
// -------------------------------------------------------------------------------------------------------------------------- private static ExceptionDetail FromXMLChild(XElement src, int version) { ExceptionDetail res = new ExceptionDetail(); res.Message = (from x in src.Elements("Message") select x.Value).SingleOrDefault(); res.Comment = (from x in src.Elements("AdditionalInfo") select x.Value).SingleOrDefault(); var traceLines = (from x in src.Elements("StackTrace") select x.Value).SingleOrDefault(); if (!string.IsNullOrEmpty(traceLines)) { var stLines = traceLines.Split(new[] { Environment.NewLine }, StringSplitOptions.None); res._StackTrace = new ReadOnlyCollection <string>(stLines); } // Inner exception. XElement innerException = null; if (version == 1) { innerException = src.Element("Exception"); if (innerException != null) { res.InnerException = FromXMLChild(innerException, version); } } else if (version == 2) { innerException = src.Element("InnerException"); if (innerException != null) { res.InnerException = FromXMLChild(innerException.Element("Exception"), version); } } else { throw new NotImplementedException(string.Format("Unsupported version! ({0})", version)); } // Inner exceptions (aggregate) if (version == 1) { // Not supported in v1. res.InnerExceptions = null; } else if (version == 2) { var aggExceptions = src.Element("InnerExceptions"); if (aggExceptions != null) { res.InnerExceptions = new List <ExceptionDetail>(); foreach (var ie in aggExceptions.Elements("Exception")) { res.InnerExceptions.Add(FromXMLChild(ie, version)); } } } else { throw new NotImplementedException(string.Format("Unsupported version! ({0})", version)); } return(res); }
// -------------------------------------------------------------------------------------------------------------------------- public static XDocument GetExceptionDetailXML(Exception ex, string additionalInfo = "") { ExceptionDetail detail = new ExceptionDetail(ex, additionalInfo); return(GetXMLInner(detail)); }