Пример #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 CreateTextWriterTest()
        {
            StringBuilder         sb        = new StringBuilder();
            StringWriter          writer    = new StringWriter(sb);
            Exception             ex        = new MockException();
            XmlExceptionFormatter formatter = new XmlExceptionFormatter(writer, ex, Guid.Empty);

            Assert.AreSame(ex, formatter.Exception);
        }
        public void CreateXmlWriterTest()
        {
            StringBuilder sb = new StringBuilder();
            XmlTextWriter writer = new XmlTextWriter(new StringWriter(sb));
            Exception ex = new MockException();
            XmlExceptionFormatter formatter = new XmlExceptionFormatter(writer, ex, Guid.Empty);

            Assert.AreSame(writer, formatter.Writer);
            Assert.AreSame(ex, formatter.Exception);
        }
        public void CreateXmlWriterTest()
        {
            StringBuilder sb = new StringBuilder();
            XmlTextWriter writer = new XmlTextWriter(new StringWriter(sb, TestUtility.DefaultCulture));

            XmlExceptionFormatter formatter = new XmlExceptionFormatter(writer, ex);

            Assert.AreSame(writer, formatter.Writer);
            Assert.AreSame(ex, formatter.Exception);
        }
        public void CreateXmlWriterTest()
        {
            StringBuilder         sb        = new StringBuilder();
            XmlTextWriter         writer    = new XmlTextWriter(new StringWriter(sb));
            Exception             ex        = new MockException();
            XmlExceptionFormatter formatter = new XmlExceptionFormatter(writer, ex);

            Assert.AreSame(writer, formatter.Writer);
            Assert.AreSame(ex, formatter.Exception);
        }
        public void CreateTextWriterTest()
        {
            StringBuilder         sb        = new StringBuilder();
            StringWriter          writer    = new StringWriter(sb);
            Exception             ex        = new MockException();
            XmlExceptionFormatter formatter = new XmlExceptionFormatter(writer, ex);

            // TextWriter won't be the same so we can only test the Exception
            Assert.AreSame(ex, formatter.Exception);
        }
Пример #7
0
        public void CreateXmlWriterTest()
        {
            StringBuilder sb     = new StringBuilder();
            XmlTextWriter writer = new XmlTextWriter(new StringWriter(sb, TestUtility.DefaultCulture));

            XmlExceptionFormatter formatter = new XmlExceptionFormatter(writer, ex);

            Assert.AreSame(writer, formatter.Writer);
            Assert.AreSame(ex, formatter.Exception);
        }
Пример #8
0
        public void CreateTextWriterTest()
        {
            StringBuilder sb     = new StringBuilder();
            StringWriter  writer = new StringWriter(sb, TestUtility.DefaultCulture);

            XmlExceptionFormatter formatter = new XmlExceptionFormatter(writer, ex);

            // TextWriter won't be the same so we can only test the Exception
            Assert.AreSame(ex, formatter.Exception);
        }
        public void CreateTextWriterTest()
        {
            StringBuilder sb = new StringBuilder();
            StringWriter writer = new StringWriter(sb);
            Exception ex = new MockException();
            XmlExceptionFormatter formatter = new XmlExceptionFormatter(writer, ex, Guid.Empty);

            // TextWriter won't be the same so we can only test the Exception
            Assert.AreSame(ex, formatter.Exception);
        }
        public void CreateTextWriterTest()
        {
            StringBuilder sb = new StringBuilder();
            StringWriter writer = new StringWriter(sb, TestUtility.DefaultCulture);

            XmlExceptionFormatter formatter = new XmlExceptionFormatter(writer, ex);

            // TextWriter won't be the same so we can only test the Exception
            Assert.AreSame(ex, formatter.Exception);
        }
Пример #11
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);
        }
Пример #12
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 SimpleXmlWriterFormatterTest()
        {
            StringBuilder sb = new StringBuilder();
            XmlTextWriter writer = new XmlTextWriter(new StringWriter(sb, TestUtility.DefaultCulture));

            XmlExceptionFormatter formatter = new XmlExceptionFormatter(writer, ex);

            Assert.IsTrue(sb.Length == 0);

            formatter.Format();

            Assert.IsTrue(sb.Length > 0);
        }
Пример #14
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());
        }
Пример #15
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);
        }
Пример #18
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);
        }
Пример #19
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);
        }
Пример #20
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 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 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 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 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);
        }