コード例 #1
0
        public void Log_ConfiguredConditionalLogLevelMapping_LogLevelNotChanged()
        {
            var logLevelDictionary = new LogLevelDictionary
            {
                {
                    CoreEventIds.BackgroundServiceStarting,
                    (exception, originalLogLevel, message) =>
                    {
                        if (exception is InvalidOperationException)
                        {
                            return(LogLevel.Error);
                        }

                        return(originalLogLevel);
                    }
                }
            };
            var internalLogger = new LoggerSubstitute <object>();
            var logger         = new SilverbackLogger <object>(internalLogger, logLevelDictionary);

            logger.Log(
                LogLevel.Information,
                CoreEventIds.BackgroundServiceStarting,
                new ArgumentException("param"),
                "Log Message");

            internalLogger.Received(LogLevel.Information, typeof(ArgumentException), "Log Message");
        }
コード例 #2
0
        public async Task HandleAsync_LogWarning_WarningIsLogged(
            IIntegrationMessage message,
            string expectedValidationMessage)
        {
            var endpoint = TestConsumerEndpoint.GetDefault();

            endpoint.MessageValidationMode = MessageValidationMode.LogWarning;

            var envelope = new InboundEnvelope(
                message,
                null,
                null,
                new TestOffset(),
                endpoint,
                "source-endpoint");

            IRawInboundEnvelope?result = null;

            await new ValidatorConsumerBehavior(_inboundLogger).HandleAsync(
                ConsumerPipelineContextHelper.CreateSubstitute(envelope, _serviceProvider),
                context =>
            {
                result = context.Envelope;
                return(Task.CompletedTask);
            });

            result.Should().NotBeNull();
            _loggerSubstitute.Received(LogLevel.Warning, null, expectedValidationMessage, 1080);
        }
コード例 #3
0
        public void Log_WithDefaultLogLevel_MessageLogged()
        {
            var services = new ServiceCollection();

            services
            .AddSingleton(typeof(ILogger <SilverbackLoggerTests>), _logger)
            .AddSilverback();

            var serviceProvider = services.BuildServiceProvider();

            var silverbackLogger = serviceProvider.GetRequiredService <ISilverbackLogger <SilverbackLoggerTests> >();

            silverbackLogger.Log(LogLevel.Information, CoreEventIds.DistributedLockAcquired, "Log Message 1");

            _logger.Received(LogLevel.Information, null, "Log Message 1");
        }
コード例 #4
0
        public async Task HandleAsync_LogWarning_WarningIsLogged(
            IIntegrationMessage message,
            string expectedValidationMessage)
        {
            var endpoint = TestProducerEndpoint.GetDefault();

            endpoint.MessageValidationMode = MessageValidationMode.LogWarning;
            var envelope = new OutboundEnvelope(message, null, endpoint);

            IOutboundEnvelope?result = null;

            await new ValidatorProducerBehavior(_outboundLogger).HandleAsync(
                new ProducerPipelineContext(
                    envelope,
                    Substitute.For <IProducer>(),
                    Substitute.For <IServiceProvider>()),
                context =>
            {
                result = context.Envelope;
                return(Task.CompletedTask);
            });

            result.Should().NotBeNull();
            result !.Message.Should().NotBeNull();
            _loggerSubstitute.Received(LogLevel.Warning, null, expectedValidationMessage, 1079);
        }
コード例 #5
0
        public void Log_ConfiguredMessageBasedLogLevelMapping_LogLevelChanged()
        {
            var logLevelDictionary = new LogLevelDictionary
            {
                {
                    CoreEventIds.BackgroundServiceStarting,
                    (exception, originalLogLevel, message) =>
                    {
                        if (message.Value == "TestMessage 10")
                        {
                            return(LogLevel.Error);
                        }

                        return(originalLogLevel);
                    }
                }
            };
            var internalLogger = new LoggerSubstitute <object>();
            var logger         = new SilverbackLogger <object>(internalLogger, logLevelDictionary);

            logger.Log(
                LogLevel.Information,
                CoreEventIds.BackgroundServiceStarting,
                new ArgumentException("param"),
                "TestMessage {Value}",
                10);

            internalLogger.Received(LogLevel.Error, typeof(ArgumentException), "TestMessage 10");
        }
コード例 #6
0
        public void Log_ConfiguredMessageBasedLogLevelMapping_LogLevelChanged()
        {
            var logLevels = new LogLevelDictionary
            {
                {
                    CoreLogEvents.DistributedLockAcquired.EventId,
                    (_, originalLogLevel, message) =>
                    {
                        if (message.Value == "Acquired lock name (id).")
                        {
                            return(LogLevel.Error);
                        }

                        return(originalLogLevel);
                    }
                }
            };
            var logger             = new LoggerSubstitute <SilverbackLoggerTests>(LogLevel.Information);
            var mappedLevelsLogger = new MappedLevelsLogger <SilverbackLoggerTests>(logger, logLevels);
            var silverbackLogger   = new SilverbackLogger <SilverbackLoggerTests>(mappedLevelsLogger);

            silverbackLogger.LogLockAcquired(new DistributedLockSettings("name", "id"));

            logger.Received(LogLevel.Error, null, "Acquired lock name (id).");
        }
コード例 #7
0
        public async Task HandleAsync_ExceptionThrown_ExceptionLogged()
        {
            var logger            = new LoggerSubstitute <FatalExceptionLoggerConsumerBehavior>();
            var integrationLogger = new SilverbackIntegrationLogger <FatalExceptionLoggerConsumerBehavior>(
                logger,
                new LogTemplates());

            var rawEnvelope = new RawInboundEnvelope(
                new byte[5],
                null,
                TestConsumerEndpoint.GetDefault(),
                TestConsumerEndpoint.GetDefault().Name,
                new TestOffset());

            try
            {
                await new FatalExceptionLoggerConsumerBehavior(integrationLogger).HandleAsync(
                    new ConsumerPipelineContext(
                        rawEnvelope,
                        Substitute.For <IConsumer>(),
                        Substitute.For <ISequenceStore>(),
                        Substitute.For <IServiceProvider>()),
                    _ => throw new InvalidCastException());
            }
            catch
            {
                // Ignored
            }

            logger.Received(LogLevel.Critical, typeof(InvalidCastException));
        }
コード例 #8
0
        public void Log_ConfiguredConditionalLogLevelMapping_LogLevelNotChanged()
        {
            var logLevels = new LogLevelDictionary
            {
                {
                    CoreLogEvents.FailedToAcquireDistributedLock.EventId,
                    (exception, originalLogLevel, _) =>
                    {
                        if (exception is InvalidCastException)
                        {
                            return(LogLevel.Error);
                        }

                        return(originalLogLevel);
                    }
                }
            };
            var logger             = new LoggerSubstitute <SilverbackLoggerTests>(LogLevel.Debug);
            var mappedLevelsLogger = new MappedLevelsLogger <SilverbackLoggerTests>(logger, logLevels);
            var silverbackLogger   = new SilverbackLogger <SilverbackLoggerTests>(mappedLevelsLogger);

            silverbackLogger.LogFailedToAcquireLock(
                new DistributedLockSettings("name", "id"),
                new InvalidOperationException());

            logger.Received(
                LogLevel.Debug,
                typeof(InvalidOperationException),
                "Failed to acquire lock name (id).");
        }
コード例 #9
0
        public void LogProduced_Envelope_Logged()
        {
            var envelope = new OutboundEnvelope(
                null,
                new MessageHeaderCollection
            {
                { DefaultMessageHeaders.MessageType, "Message.Type" },
                { DefaultMessageHeaders.MessageId, "1234" },
                { KafkaMessageHeaders.KafkaMessageKey, "key1234" }
            },
                new KafkaProducerEndpoint("test1"),
                true,
                new KafkaOffset("topic2", 2, 42));

            var expectedMessage =
                "Message produced. | " +
                "endpointName: test1, " +
                "messageType: Message.Type, " +
                "messageId: 1234, " +
                "offset: [2]@42, " +
                "kafkaKey: key1234";

            _outboundLogger.LogProduced(envelope);

            _loggerSubstitute.Received(LogLevel.Information, null, expectedMessage, 1031);
        }
コード例 #10
0
        public void Log_EmptyLogLevelMapping_LogLevelNotChanged()
        {
            var logLevelDictionary = new LogLevelDictionary();
            var internalLogger     = new LoggerSubstitute <object>();
            var logger             = new SilverbackLogger <object>(internalLogger, logLevelDictionary);

            logger.Log(LogLevel.Information, CoreEventIds.BackgroundServiceStarting, "Log Message");

            internalLogger.Received(LogLevel.Information, null, "Log Message");
        }
コード例 #11
0
        public void Log_EmptyLogLevelMapping_LogLevelNotChanged()
        {
            var logLevels          = new LogLevelDictionary();
            var logger             = new LoggerSubstitute <SilverbackLoggerTests>(LogLevel.Information);
            var mappedLevelsLogger =
                new MappedLevelsLogger <SilverbackLoggerTests>(logger, logLevels);
            var silverbackLogger = new SilverbackLogger <SilverbackLoggerTests>(mappedLevelsLogger);

            silverbackLogger.LogLockAcquired(new DistributedLockSettings("name", "id"));

            logger.Received(LogLevel.Information, null, "Acquired lock name (id).");
        }
コード例 #12
0
        public void LogProduced_Envelope_Logged()
        {
            var envelope = new OutboundEnvelope(
                null,
                new MessageHeaderCollection
            {
                { DefaultMessageHeaders.MessageType, "Message.Type" },
                { DefaultMessageHeaders.MessageId, "1234" }
            },
                new TestProducerEndpoint("test1"),
                true,
                new TestOffset("a", "42"));

            var expectedMessage =
                "Message produced. | " +
                "endpointName: test1, " +
                "messageType: Message.Type, " +
                "messageId: 1234, " +
                "unused1: (null), " +
                "unused2: (null)";

            _outboundLogger.LogProduced(envelope);

            _loggerSubstitute.Received(LogLevel.Information, null, expectedMessage, 1031);
        }
コード例 #13
0
        public void Log_ConfiguredLogLevelMapping_LogLevelChanged()
        {
            var logLevelDictionary = new LogLevelDictionary
            {
                { CoreEventIds.BackgroundServiceStarting, (_, _, _) => LogLevel.Error }
            };
            var internalLogger = new LoggerSubstitute <object>();
            var logger         = new SilverbackLogger <object>(internalLogger, logLevelDictionary);

            logger.Log(LogLevel.Information, CoreEventIds.BackgroundServiceStarting, "Log Message");

            internalLogger.Received(LogLevel.Error, null, "Log Message");
        }
コード例 #14
0
        public void Log_EventIdNotConfigured_LogLevelNotChanged()
        {
            var logLevelDictionary = new LogLevelDictionary
            {
                { CoreEventIds.BackgroundServiceStarting, (e, l, m) => LogLevel.Error }
            };
            var internalLogger = new LoggerSubstitute <object>();
            var logger         = new SilverbackLogger <object>(internalLogger, logLevelDictionary);

            logger.Log(LogLevel.Information, CoreEventIds.BackgroundServiceLockAcquired, "Log Message");

            internalLogger.Received(LogLevel.Information, null, "Log Message");
        }
コード例 #15
0
        public void Log_EventIdNotConfigured_LogLevelNotChanged()
        {
            var logLevels = new LogLevelDictionary
            {
                { CoreLogEvents.BackgroundServiceStarting.EventId, (_, _, _) => LogLevel.Error }
            };
            var logger             = new LoggerSubstitute <SilverbackLoggerTests>(LogLevel.Information);
            var mappedLevelsLogger = new MappedLevelsLogger <SilverbackLoggerTests>(logger, logLevels);
            var silverbackLogger   = new SilverbackLogger <SilverbackLoggerTests>(mappedLevelsLogger);

            silverbackLogger.LogLockAcquired(new DistributedLockSettings("name", "id"));

            logger.Received(LogLevel.Information, null, "Acquired lock name (id).");
        }
コード例 #16
0
        public void ShouldLogExceptionIfSslRequiredButCertificateHasNotBeenSet()
        {
            var message = new MailMessage(_fromAddress, _toAddress)
            {
                Subject = "Subject of email",
                Body    = "Body of the best email ever."
            };

            message.To.Add(_toAddress);
            message.CC.Add(_toAddress2);

            Action send = () => SendMessage(message, true);

            send.ShouldThrow <SmtpException>().WithMessage("Server does not support secure connections.");
            LoggerSubstitute.Received(1).LogWarning("Stream was closed before QUIT command from server:\nClient has disconnected unexpectedly.");
        }
コード例 #17
0
        public void LogProcessing_Logged()
        {
            var envelope = new RawInboundEnvelope(
                Stream.Null,
                new MessageHeaderCollection
            {
                { DefaultMessageHeaders.MessageType, "Message.Type" },
                { DefaultMessageHeaders.MessageId, "1234" },
                { KafkaMessageHeaders.KafkaMessageKey, "key1234" }
            },
                new KafkaConsumerEndpoint("topic1", "topic2"),
                "topic2",
                new KafkaOffset("topic2", 2, 42));

            var expectedMessage =
                "Processing inbound message. | " +
                "endpointName: topic2, " +
                "messageType: Message.Type, " +
                "messageId: 1234, " +
                "offset: [2]@42, " +
                "kafkaKey: key1234";

            _inboundLogger.LogProcessing(envelope);

            _loggerSubstitute.Received(LogLevel.Information, null, expectedMessage, 1001);
        }
コード例 #18
0
        public void LogMessageAddedToSequence_Logged()
        {
            var expectedMessage = "Message '1234' added to FakeSequence 'fake1'. | length: 3";

            _silverbackLogger.LogMessageAddedToSequence(
                new RawInboundEnvelope(
                    Stream.Null,
                    new MessageHeaderCollection
            {
                { DefaultMessageHeaders.MessageId, "1234" }
            },
                    TestConsumerEndpoint.GetDefault(),
                    "test",
                    new TestOffset()),
                new FakeSequence());

            _loggerSubstitute.Received(LogLevel.Debug, null, expectedMessage, 1003);
        }
コード例 #19
0
        public void LogProcessing_SingleInboundMessage_InformationLogged()
        {
            _integrationLogger.LogProcessing(_singleInboundMessageContext);

            const string expectedMessage =
                "Processing inbound message. | " +
                "endpointName: TestActual, " +
                "failedAttempts: 1, " +
                "messageType: Something.Xy, " +
                "messageId: 1234, " +
                "offset-in: 9";

            _logger.Received(LogLevel.Information, null, expectedMessage);
        }
コード例 #20
0
        public void LogProcessing_Logged()
        {
            var envelope = new RawInboundEnvelope(
                Stream.Null,
                new MessageHeaderCollection
            {
                { DefaultMessageHeaders.MessageType, "Message.Type" },
                { DefaultMessageHeaders.MessageId, "1234" }
            },
                new TestConsumerEndpoint("test1, test2"),
                "test1",
                new TestOffset("a", "42"));

            var expectedMessage =
                "Processing inbound message. | " +
                "endpointName: test1, " +
                "messageType: Message.Type, " +
                "messageId: 1234, " +
                "unused1: (null), " +
                "unused2: (null)";

            _inboundLogger.LogProcessing(envelope);

            _loggerSubstitute.Received(LogLevel.Information, null, expectedMessage, 1001);
        }