예제 #1
0
        /// <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++;
        }
예제 #2
0
        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);
        }
예제 #3
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());
        }
예제 #4
0
        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());
        }
예제 #5
0
        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 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);
        }
예제 #9
0
        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);
        }
예제 #10
0
        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);
        }
예제 #11
0
        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);
        }
        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());
        }