Beispiel #1
0
        public void Log_WithFailingProvider_LogsToRemainingLoggers()
        {
            // Arrange
            var logger1 = new FailingLoggingProvider("Failure")
            {
                ExceptionToThrow = new Exception()
            };
            var logger2             = CreateInitializedMemoryLogger("MemoryLogger");
            var configuredProviders = new LoggingProviderCollection()
            {
                logger1, logger2
            };
            var providerUnderTest = CreateInitializedCompositeLoggingProvider(configuredProviders);
            var expectedMessage   = "Some message";

            // Act
            try
            {
                providerUnderTest.Log(expectedMessage);

                // Assert
                Assert.Fail("An exception was expected to be thrown.");
            }
            catch
            {
                // We're not interested in the exception
            }

            Assert.AreEqual(1, logger2.GetLoggedEntries().Length);
            Assert.AreEqual(expectedMessage, logger2.GetLoggedEntries().First().Message);
        }
Beispiel #2
0
        public void Log_WithFailingProviders_ThrowsExceptionWithExpectedTypeAndMessage()
        {
            // Arrange
            var logger1 = new FailingLoggingProvider("Faile1")
            {
                ExceptionToThrow = new Exception("foo")
            };
            var logger2 = new FailingLoggingProvider("Faile2")
            {
                ExceptionToThrow = new Exception("bar")
            };
            var logger3             = CreateInitializedMemoryLogger("MemoryLogger");
            var configuredProviders = new LoggingProviderCollection()
            {
                logger1, logger2, logger3
            };
            var providerUnderTest = CreateInitializedCompositeLoggingProvider(configuredProviders);
            var expectedMessage   = "Some message";

            // Act
            try
            {
                providerUnderTest.Log(expectedMessage);

                // Assert
                Assert.Fail("An exception was expected to be thrown.");
            }
            catch (Exception ex)
            {
                // When logging to multiple providers, the provider should wrap the thrown exceptions in a
                // CompositeException, even if there is only one Exception (re throwing the same exception
                // would make us loose the stack trace).
                Assert.IsInstanceOfType(ex, typeof(CompositeException));
                Assert.IsTrue(ex.Message.Contains("foo"),
                              "Exception message should contain all inner exception messages. (foo missing)");
                Assert.IsTrue(ex.Message.Contains("bar"),
                              "Exception message should contain all inner exception messages. (bar missing)");
            }
        }