public static void Convert_Exception_To_XML() { DivideByZeroException outerException = new DivideByZeroException(); ExceptionXElement xmlRaw; { try { var path = File.ReadAllLines("file does not exist"); } catch (Exception ex) { outerException = new DivideByZeroException("Outer exception", ex); } xmlRaw = new ExceptionXElement(outerException); } var xml = xmlRaw.ToString(); Assert.IsTrue(xml.ToLower().Contains("exception")); // TODO: Comment this in once .NET Core 1.1 is released. // Assert.IsTrue(xml.ToLower().Contains("DivideByZeroException")); Assert.IsTrue(xml.Contains("<")); Assert.IsTrue(xml.Contains(">")); }
public static void Main(string[] args) { try { //throw an DivideByZeroException var a = 0; var b = 1 / a; } catch (Exception ex) { //using the ExceptionXElement class var xmlException = new ExceptionXElement(ex); XslCompiledTransform myXslTrans = new XslCompiledTransform(); //Resources.formatter is the xsl file added as a Resource to the project (ConsoleApplication2.Properties.Resources.formatter) //So, here we load the xsl myXslTrans.Load(XmlReader.Create(new StringReader(Resources.formatter))); //initialize a TextWriter, in this case a StringWriter and set it to write to a StringBuilder StringBuilder stringBuilder = new StringBuilder(); XmlTextWriter myWriter = new XmlTextWriter(new StringWriter(stringBuilder)); //apply the XSL transformations to the xmlException and output them to the XmlWriter myXslTrans.Transform(xmlException.CreateReader(), null, myWriter); //outputting to the console the HTML exception (you can send it as the message body of an email) Console.WriteLine(stringBuilder); } }
internal static string DumpException(Exception exception, string filenamePrefix) { DateTime time = DateTime.UtcNow; string timeString = time.ToString("o"); string filename = filenamePrefix + "-" + timeString.Replace(':', ';') + "-" + (_dumpCounter++).ToString("#####") + ".xml"; string path = Path.Combine(Environment.CurrentDirectory, "dumps", filename); ExceptionXElement exceptionElement = new ExceptionXElement(exception); exceptionElement.Add(new XElement("Time", timeString)); exceptionElement.Add(new XElement("Assemblies", AppDomain.CurrentDomain.GetAssemblies().Select(assembly => new XElement("Assembly", assembly.FullName)))); XDocument document = new XDocument(); document.Add(exceptionElement); document.Save(path); return path; }
internal static string DumpException(Exception exception, string filenamePrefix) { DateTime time = DateTime.UtcNow; string timeString = time.ToString("o"); string filename = filenamePrefix + "-" + timeString.Replace(':', ';') + "-" + (_dumpCounter++).ToString("#####") + ".xml"; string path = Path.Combine(Environment.CurrentDirectory, "dumps", filename); ExceptionXElement exceptionElement = new ExceptionXElement(exception); exceptionElement.Add(new XElement("Time", timeString)); exceptionElement.Add(new XElement("Assemblies", AppDomain.CurrentDomain.GetAssemblies().Select(assembly => new XElement("Assembly", assembly.FullName)))); XDocument document = new XDocument(); document.Add(exceptionElement); document.Save(path); return(path); }