[Ignore]    // TODO remove with instrumentation
        public void LogHandlingError()
        {
            if (!EventLog.SourceExists(EventLogSource)) return;

            Exception ex = new Exception(exceptionMessage);
            ExceptionUtility.FormatExceptionHandlingExceptionMessage(policy, null, null, ex);

            StringBuilder message = new StringBuilder();
            StringWriter writer = null;
            try
            {
                writer = new StringWriter(message);
                TextExceptionFormatter formatter = new TextExceptionFormatter(writer, ex);
                formatter.Format();
            }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                }
            }

            using (EventLog log = new EventLog(EventLogName))
            {
                EventLogEntry entry = log.Entries[log.Entries.Count - 1];

                Assert.AreEqual(EventLogEntryType.Error, entry.EntryType);
                Assert.AreEqual(EventLogSource, entry.Source);
            }
        }
        public void AdditionalInfoTest()
        {
            StringBuilder sb     = new StringBuilder();
            StringWriter  writer = new StringWriter(sb);

            Exception exception = new FileNotFoundException(fileNotFoundMessage, theFile);
            TextExceptionFormatter formatter = new TextExceptionFormatter(writer, exception);

            formatter.Format();

            if (string.Compare(permissionDenied, formatter.AdditionalInfo[machineName]) != 0)
            {
                Assert.AreEqual(Environment.MachineName, formatter.AdditionalInfo[machineName]);
            }

            DateTime minimumTime = DateTime.UtcNow.AddMinutes(-1);
            DateTime loggedTime  = DateTime.Parse(formatter.AdditionalInfo[timeStamp]);

            if (DateTime.Compare(minimumTime, loggedTime) > 0)
            {
                Assert.Fail(loggedTimeStampFailMessage);
            }

            Assert.AreEqual(AppDomain.CurrentDomain.FriendlyName, formatter.AdditionalInfo[appDomainName]);
            Assert.AreEqual(Thread.CurrentPrincipal.Identity.Name, formatter.AdditionalInfo[threadIdentity]);

            if (string.Compare(permissionDenied, formatter.AdditionalInfo[windowsIdentity]) != 0)
            {
                Assert.AreEqual(WindowsIdentity.GetCurrent().Name, formatter.AdditionalInfo[windowsIdentity]);
            }
        }
        public void LogHandlingError()
        {
            if (!EventLog.SourceExists(EventLogSource))
            {
                return;
            }
            Exception ex = new Exception(exceptionMessage);

            ExceptionUtility.FormatExceptionHandlingExceptionMessage(policy, null, null, ex);
            StringBuilder message = new StringBuilder();
            StringWriter  writer  = null;

            try
            {
                writer = new StringWriter(message);
                TextExceptionFormatter formatter = new TextExceptionFormatter(writer, ex);
                formatter.Format();
            }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                }
            }
            using (EventLog log = new EventLog(EventLogName))
            {
                EventLogEntry entry = log.Entries[log.Entries.Count - 1];
                Assert.AreEqual(EventLogEntryType.Error, entry.EntryType);
                Assert.AreEqual(EventLogSource, entry.Source);
            }
        }
예제 #4
0
        IEnumerable <IExceptionPublisher> InitializeExceptionPublishers()
        {
            var exceptionFormatter   = new TextExceptionFormatter();
            var configuredPublishers = AppLogic.AppConfig("System.LoggingLocation");

            if (CommonLogic.StringInCommaDelimitedStringList("file", configuredPublishers))
            {
                yield return(new FileBasedExceptionPublisher(exceptionFormatter));
            }

            if (CommonLogic.StringInCommaDelimitedStringList("email", configuredPublishers))
            {
                yield return(new EmailExceptionPublisher(exceptionFormatter));
            }

            if (CommonLogic.StringInCommaDelimitedStringList("eventLog", configuredPublishers))
            {
                yield return(new EventLogExceptionPublisher(exceptionFormatter));
            }

            if (CommonLogic.StringInCommaDelimitedStringList("database", configuredPublishers))
            {
                yield return(new DatabaseExceptionPublisher());
            }
        }
        public void AdditionalInfoTest()
        {
            StringBuilder sb = new StringBuilder();
            StringWriter writer = new StringWriter(sb);

            Exception exception = new FileNotFoundException(fileNotFoundMessage, theFile);
            TextExceptionFormatter formatter = new TextExceptionFormatter(writer, exception);

            formatter.Format();

            if (string.Compare(permissionDenied, formatter.AdditionalInfo[machineName]) != 0)
            {
                Assert.AreEqual(Environment.MachineName, formatter.AdditionalInfo[machineName]);
            }

            DateTime minimumTime = DateTime.UtcNow.AddMinutes(-1);
            DateTime loggedTime = DateTime.Parse(formatter.AdditionalInfo[timeStamp]);
            if (DateTime.Compare(minimumTime, loggedTime) > 0)
            {
                Assert.Fail(loggedTimeStampFailMessage);
            }

            Assert.AreEqual(AppDomain.CurrentDomain.FriendlyName, formatter.AdditionalInfo[appDomainName]);
            Assert.AreEqual(Thread.CurrentPrincipal.Identity.Name, formatter.AdditionalInfo[threadIdentity]);

            if (string.Compare(permissionDenied, formatter.AdditionalInfo[windowsIdentity]) != 0)
            {
                Assert.AreEqual(WindowsIdentity.GetCurrent().Name, formatter.AdditionalInfo[windowsIdentity]);
            }
        }
        public void CreateTest()
        {
            TextWriter writer = new StringWriter();
            Exception exception = new Exception();
            TextExceptionFormatter formatter = new TextExceptionFormatter(writer, exception);

            Assert.AreSame(writer, formatter.Writer);
            Assert.AreSame(exception, formatter.Exception);
        }
        public void CreateTest()
        {
            TextWriter             writer    = new StringWriter();
            Exception              exception = new Exception();
            TextExceptionFormatter formatter = new TextExceptionFormatter(writer, exception);

            Assert.AreSame(writer, formatter.Writer);
            Assert.AreSame(exception, formatter.Exception);
        }
        public void VerifyInnerExceptionGetsFormatted()
        {
            StringBuilder          sb        = new StringBuilder();
            StringWriter           writer    = new StringWriter(sb);
            Exception              exception = new MockException(message, new MockException());
            TextExceptionFormatter formatter = new TextExceptionFormatter(writer, exception);

            Assert.IsTrue(sb.Length == 0);
            formatter.Format();
            Assert.IsTrue(sb.ToString().Contains(innerException));
        }
        public void SimpleFormatterTest()
        {
            StringBuilder          sb        = new StringBuilder();
            StringWriter           writer    = new StringWriter(sb);
            Exception              exception = new MockException();
            TextExceptionFormatter formatter = new TextExceptionFormatter(writer, exception);

            Assert.IsTrue(sb.Length == 0);
            formatter.Format();
            Assert.IsTrue(sb.Length > 0);
        }
        public void SkipsHandlingInstanceIdIfEmpty()
        {
            StringBuilder          sb        = new StringBuilder();
            StringWriter           writer    = new StringWriter(sb);
            Exception              exception = new MockException();
            TextExceptionFormatter formatter = new TextExceptionFormatter(writer, exception, Guid.Empty);

            formatter.Format();
            string[] lines = sb.ToString().Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
            Assert.IsFalse(lines.Any(l => l.StartsWith("HandlingInstanceID", StringComparison.Ordinal)));
        }
            public string DoTest()
            {
                StringBuilder          sb        = new StringBuilder();
                StringWriter           writer    = new StringWriter(sb);
                Exception              exception = new MockException();
                TextExceptionFormatter formatter = new TextExceptionFormatter(writer, exception);

                formatter.Format();

                return(sb.ToString());
            }
		public void VerifyInnerExceptionGetsFormatted()
		{
			StringBuilder sb = new StringBuilder();
			StringWriter writer = new StringWriter(sb);
			Exception exception = new MockException(message, new MockException());

			TextExceptionFormatter formatter = new TextExceptionFormatter(writer, exception);
			Assert.IsTrue(sb.Length == 0);
			formatter.Format();

			Assert.IsTrue(sb.ToString().Contains(innerException));
		}
예제 #13
0
        private string SerializeException(Exception ex)
        {
            string ret;

            using (var sr = new StringWriter())
            {
                var formatter = new TextExceptionFormatter(sr, ex);
                formatter.Format();
                ret = sr.ToString();
            }
            return(ret);
        }
        private static string CreateExceptionDetails(Exception e)
        {
            if (e == null)
            {
                return(string.Empty);
            }

            using (var textWriter = new StringWriter())
            {
                var formatter = new TextExceptionFormatter(textWriter, e);
                formatter.Format();
                return(textWriter.ToString());
            }
        }
        public void WritesHandlingInstanceIdIfNotEmpty()
        {
            StringBuilder          sb        = new StringBuilder();
            StringWriter           writer    = new StringWriter(sb);
            Guid                   testGuid  = Guid.NewGuid();
            Exception              exception = new MockException();
            TextExceptionFormatter formatter = new TextExceptionFormatter(writer, exception, testGuid);

            formatter.Format();
            string[] lines = sb.ToString().Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
            string   line  = lines.First(l => l.StartsWith("HandlingInstanceID", StringComparison.Ordinal));

            Assert.IsNotNull(line);
            Assert.IsTrue(line.IndexOf(testGuid.ToString("D", CultureInfo.InvariantCulture)) >= 0);
        }
        public void CanGetMachineNameWithoutSecurity()
        {
            EnvironmentPermission denyPermission = new EnvironmentPermission(EnvironmentPermissionAccess.Read, computerName);
            PermissionSet         permissions    = new PermissionSet(PermissionState.None);

            permissions.AddPermission(denyPermission);
            permissions.Deny();

            StringBuilder          sb        = new StringBuilder();
            StringWriter           writer    = new StringWriter(sb);
            Exception              exception = new MockException();
            TextExceptionFormatter formatter = new TextExceptionFormatter(writer, exception);

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

            Assert.IsTrue(sb.ToString().Contains(machineName + " : " + permissionDenied));
        }
        public void CanGetWindowsIdentityWithoutSecurity()
        {
            SecurityPermission denyPermission = new SecurityPermission(SecurityPermissionFlag.ControlPrincipal);
            PermissionSet      permissions    = new PermissionSet(PermissionState.None);

            permissions.AddPermission(denyPermission);
            permissions.Deny();

            StringBuilder          sb        = new StringBuilder();
            StringWriter           writer    = new StringWriter(sb);
            Exception              exception = new MockException();
            TextExceptionFormatter formatter = new TextExceptionFormatter(writer, exception);

            Assert.IsTrue(sb.Length == 0);
            formatter.Format();
            Console.WriteLine(sb.ToString());
            Assert.IsTrue(sb.ToString().Contains(windowsIdentity + " : " + permissionDenied));
        }
        public void SimpleFormatterTest()
        {
            StringBuilder sb = new StringBuilder();
            StringWriter writer = new StringWriter(sb);

            Exception exception = new MockException();

            TextExceptionFormatter formatter = new TextExceptionFormatter(writer, exception);

            // Nothing should be written until Format() is called
            Assert.IsTrue(sb.Length == 0);

            // Format the exception
            formatter.Format();

            // Not much of a test, but at least we can tell if _something_ got written
            // to the underlying StringBuilder
            Assert.IsTrue(sb.Length > 0);
        }
        public void SimpleFormatterTest()
        {
            StringBuilder sb     = new StringBuilder();
            StringWriter  writer = new StringWriter(sb);

            Exception exception = new MockException();

            TextExceptionFormatter formatter = new TextExceptionFormatter(writer, exception);

            // Nothing should be written until Format() is called
            Assert.IsTrue(sb.Length == 0);

            // Format the exception
            formatter.Format();

            // Not much of a test, but at least we can tell if _something_ got written
            // to the underlying StringBuilder
            Assert.IsTrue(sb.Length > 0);
        }
예제 #20
0
        public void AdditionalInfoTest()
        {
            StringBuilder sb = new StringBuilder();
            StringWriter writer = new StringWriter(sb, TestUtility.DefaultCulture);

            Exception exception = new FileNotFoundException("The file can't be found", "theFile");
            TextExceptionFormatter formatter = new TextExceptionFormatter(writer, exception);

            formatter.Format();

            Assert.AreEqual(Environment.MachineName, formatter.AdditionalInfo["MachineName"]);

            DateTime minimumTime = DateTime.Now.AddMinutes(-1);
            DateTime loggedTime = DateTime.Parse(formatter.AdditionalInfo["TimeStamp"]);
            if (DateTime.Compare(minimumTime, loggedTime) > 0)
            {
                Assert.Fail("Logged TimeStamp is not within a one minute time window");
            }

            Assert.AreEqual(AppDomain.CurrentDomain.FriendlyName, formatter.AdditionalInfo["AppDomainName"]);
            Assert.AreEqual(Thread.CurrentPrincipal.Identity.Name, formatter.AdditionalInfo["ThreadIdentity"]);
            Assert.AreEqual(WindowsIdentity.GetCurrent().Name, formatter.AdditionalInfo["WindowsIdentity"]);
        }
예제 #21
0
        public void AdditionalInfoTest()
        {
            StringBuilder sb     = new StringBuilder();
            StringWriter  writer = new StringWriter(sb, TestUtility.DefaultCulture);

            Exception exception = new FileNotFoundException("The file can't be found", "theFile");
            TextExceptionFormatter formatter = new TextExceptionFormatter(writer, exception);

            formatter.Format();

            Assert.AreEqual(Environment.MachineName, formatter.AdditionalInfo["MachineName"]);

            DateTime minimumTime = DateTime.Now.AddMinutes(-1);
            DateTime loggedTime  = DateTime.Parse(formatter.AdditionalInfo["TimeStamp"]);

            if (DateTime.Compare(minimumTime, loggedTime) > 0)
            {
                Assert.Fail("Logged TimeStamp is not within a one minute time window");
            }

            Assert.AreEqual(AppDomain.CurrentDomain.FriendlyName, formatter.AdditionalInfo["AppDomainName"]);
            Assert.AreEqual(Thread.CurrentPrincipal.Identity.Name, formatter.AdditionalInfo["ThreadIdentity"]);
            Assert.AreEqual(WindowsIdentity.GetCurrent().Name, formatter.AdditionalInfo["WindowsIdentity"]);
        }
        public void SkipsHandlingInstanceIdIfEmpty()
        {
            StringBuilder sb = new StringBuilder();
            StringWriter writer = new StringWriter(sb);

            Exception exception = new MockException();

            TextExceptionFormatter formatter = new TextExceptionFormatter(writer, exception, Guid.Empty);

            // Format the exception
            formatter.Format();

            string[] lines = sb.ToString().Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
            Assert.IsFalse(lines.Any(l => l.StartsWith("HandlingInstanceID", StringComparison.Ordinal)));
        }
		public void CanGetWindowsIdentityWithoutSecurity()
		{
			SecurityPermission denyPermission = new SecurityPermission(SecurityPermissionFlag.ControlPrincipal);
			PermissionSet permissions = new PermissionSet(PermissionState.None);
			permissions.AddPermission(denyPermission);
			permissions.Deny();

			StringBuilder sb = new StringBuilder();
			StringWriter writer = new StringWriter(sb);
			Exception exception = new MockException();
			TextExceptionFormatter formatter = new TextExceptionFormatter(writer, exception);
			Assert.IsTrue(sb.Length == 0);
			formatter.Format();
			Console.WriteLine(sb.ToString());
			Assert.IsTrue(sb.ToString().Contains(windowsIdentity + " : " + permissionDenied));
		}				
		public void CanGetMachineNameWithoutSecurity()
		{
			EnvironmentPermission denyPermission = new EnvironmentPermission(EnvironmentPermissionAccess.Read, computerName);
			PermissionSet permissions = new PermissionSet(PermissionState.None);
			permissions.AddPermission(denyPermission);
			permissions.Deny();

			StringBuilder sb = new StringBuilder();
			StringWriter writer = new StringWriter(sb);
			Exception exception = new MockException();
			TextExceptionFormatter formatter = new TextExceptionFormatter(writer, exception);
			Assert.IsTrue(sb.Length == 0);
			formatter.Format();

			Assert.IsTrue(sb.ToString().Contains(machineName + " : " + permissionDenied));
		}
        public void WritesHandlingInstanceIdIfNotEmpty()
        {
            StringBuilder sb = new StringBuilder();
            StringWriter writer = new StringWriter(sb);
            Guid testGuid = Guid.NewGuid();

            Exception exception = new MockException();

            TextExceptionFormatter formatter = new TextExceptionFormatter(writer, exception, testGuid);

            // Format the exception
            formatter.Format();

            string[] lines = sb.ToString().Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
            string line = lines.First(l => l.StartsWith("HandlingInstanceID", StringComparison.Ordinal));

            Assert.IsNotNull(line);
            Assert.IsTrue(line.IndexOf(testGuid.ToString("D", CultureInfo.InvariantCulture)) >= 0);
        }
            public string DoTest()
            {
                StringBuilder sb = new StringBuilder();
                StringWriter writer = new StringWriter(sb);
                Exception exception = new MockException();
                TextExceptionFormatter formatter = new TextExceptionFormatter(writer, exception);
                formatter.Format();

                return sb.ToString();
            }