/// <summary> /// Format and write an exception to the log. /// </summary> /// <param name="ex"></param> public static void Write(Exception ex) { if (ex.Data.Contains(IncidentKey)) { //exception has already been logged further up the chain so do not write again. return; } string message = ex.Message; ex.Data.Add(IncidentKey, incidentId);//add an incidentId to the exception so it can be identified if logged by more than 1 handler try { System.IO.StringWriter str = new System.IO.StringWriter(); XmlExceptionFormatter xmlFormat = new XmlExceptionFormatter(str, ex); xmlFormat.Format(); message += str.ToString(); str.Close(); } catch (Exception formattingEx) { message += "Couldn't format exception: " + formattingEx.Message; } MGRELog.Write(message, getCallingMethodName(), TraceLevel.Error); incidentId++; }
public void SimpleTextWriterFormatterTest() { StringBuilder sb = new StringBuilder(); StringWriter writer = new StringWriter(sb); Exception ex = new MockException(); XmlExceptionFormatter formatter = new XmlExceptionFormatter(writer, ex, Guid.Empty); Assert.IsTrue(sb.Length == 0); formatter.Format(); Assert.IsTrue(sb.Length > 0); }
public void WellFormedTest() { StringBuilder sb = new StringBuilder(); StringWriter writer = new StringWriter(sb); Exception ex = new MockException(); XmlExceptionFormatter formatter = new XmlExceptionFormatter(writer, ex, Guid.Empty); formatter.Format(); XmlDocument doc = new XmlDocument(); doc.LoadXml(sb.ToString()); }
public void WellFormedTest() { StringBuilder sb = new StringBuilder(); StringWriter writer = new StringWriter(sb, TestUtility.DefaultCulture); XmlExceptionFormatter formatter = new XmlExceptionFormatter(writer, ex); formatter.Format(); XmlDocument doc = new XmlDocument(); doc.LoadXml(sb.ToString()); }
public void SimpleTextWriterFormatterTest() { StringBuilder sb = new StringBuilder(); StringWriter writer = new StringWriter(sb, TestUtility.DefaultCulture); XmlExceptionFormatter formatter = new XmlExceptionFormatter(writer, ex); Assert.IsTrue(sb.Length == 0); formatter.Format(); Assert.IsTrue(sb.Length > 0); }
public void CreatesXmlExceptionFormatter() { TextWriter writer = new System.IO.StringWriter(); Exception exception = new Exception("test exception"); Guid handlingInstanceID = Guid.NewGuid(); XmlExceptionFormatter formatter = new XmlExceptionFormatter(writer, exception, handlingInstanceID); formatter.Format(); XmlDocument document = new XmlDocument(); ExceptionAssertHelper.DoesNotThrow(() => document.LoadXml(writer.ToString())); }
public void VerifyInnerExceptionGetsFormatted() { StringBuilder sb = new StringBuilder(); StringWriter writer = new StringWriter(sb); Exception exception = new MockException("exception message", new MockException()); XmlExceptionFormatter formatter = new XmlExceptionFormatter(writer, exception, Guid.Empty); Assert.IsTrue(sb.Length == 0); formatter.Format(); XmlDocument doc = new XmlDocument(); doc.LoadXml(sb.ToString()); XmlNode element = doc.DocumentElement.SelectSingleNode("//InnerException"); Assert.IsNotNull(element); }
public void VerifyInnerExceptionGetsFormatted() { StringBuilder sb = new StringBuilder(); StringWriter writer = new StringWriter(sb); Exception exception = new MockException("Foo Bar", new MockException()); XmlExceptionFormatter formatter = new XmlExceptionFormatter(writer, exception, Guid.Empty); Assert.IsTrue(sb.Length == 0); formatter.Format(); XmlDocument doc = new XmlDocument(); doc.LoadXml(sb.ToString()); XmlNode element = doc.DocumentElement.SelectSingleNode("//InnerException"); Assert.IsNotNull(element); }
public void DoesNotFormatHandlingInstanceIdIfEmpty() { StringBuilder sb = new StringBuilder(); StringWriter writer = new StringWriter(sb); Exception exception = new MockException("Foo Bar", new MockException()); XmlExceptionFormatter formatter = new XmlExceptionFormatter(writer, exception, Guid.Empty); Assert.IsTrue(sb.Length == 0); formatter.Format(); XmlDocument doc = new XmlDocument(); doc.LoadXml(sb.ToString()); XmlNode element = doc.DocumentElement.SelectSingleNode("/Exception/@handlingInstanceId"); Assert.IsNull(element); }
public void FormatsHandlingInstanceIdIfAvailable() { StringBuilder sb = new StringBuilder(); StringWriter writer = new StringWriter(sb); Exception exception = new MockException("Foo Bar", new MockException()); Guid testGuid = Guid.NewGuid(); XmlExceptionFormatter formatter = new XmlExceptionFormatter(writer, exception, testGuid); Assert.IsTrue(sb.Length == 0); formatter.Format(); XmlDocument doc = new XmlDocument(); doc.LoadXml(sb.ToString()); XmlNode element = doc.DocumentElement.SelectSingleNode("/Exception/@handlingInstanceId"); Assert.IsNotNull(element); Assert.AreEqual(testGuid.ToString("D", CultureInfo.InvariantCulture), element.InnerText); }
public void SimpleXmlWriterFormatterTest() { StringBuilder sb = new StringBuilder(); XmlTextWriter writer = new XmlTextWriter(new StringWriter(sb)); Exception ex = new MockException(); XmlExceptionFormatter formatter = new XmlExceptionFormatter(writer, ex, Guid.Empty); Assert.IsTrue(sb.Length == 0); formatter.Format(); Assert.IsTrue(sb.Length > 0); }
public void DoesNotFormatHandlingInstanceIdIfEmpty() { StringBuilder sb = new StringBuilder(); StringWriter writer = new StringWriter(sb); Exception exception = new MockException("exception message", new MockException()); XmlExceptionFormatter formatter = new XmlExceptionFormatter(writer, exception, Guid.Empty); Assert.IsTrue(sb.Length == 0); formatter.Format(); XmlDocument doc = new XmlDocument(); doc.LoadXml(sb.ToString()); XmlNode element = doc.DocumentElement.SelectSingleNode("/Exception/@handlingInstanceId"); Assert.IsNull(element); }
public void FormatsHandlingInstanceIdIfAvailable() { StringBuilder sb = new StringBuilder(); StringWriter writer = new StringWriter(sb); Exception exception = new MockException("exception message", new MockException()); Guid testGuid = Guid.NewGuid(); XmlExceptionFormatter formatter = new XmlExceptionFormatter(writer, exception, testGuid); Assert.IsTrue(sb.Length == 0); formatter.Format(); XmlDocument doc = new XmlDocument(); doc.LoadXml(sb.ToString()); XmlNode element = doc.DocumentElement.SelectSingleNode("/Exception/@handlingInstanceId"); Assert.IsNotNull(element); Assert.AreEqual(testGuid.ToString("D", CultureInfo.InvariantCulture), element.InnerText); }