Exemplo n.º 1
0
        public void LogInboundTrace_NoException_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 =
                "The RetryErrorPolicy will be skipped because the current failed " +
                "attempts (5) exceeds the configured maximum attempts (3). | " +
                "endpointName: test1, " +
                "messageType: Message.Type, " +
                "messageId: 1234, " +
                "unused1: (null), " +
                "unused2: (null)";

            _inboundLogger.LogInboundTrace(
                IntegrationLogEvents.PolicyMaxFailedAttemptsExceeded,
                envelope,
                () => new object?[] { nameof(RetryErrorPolicy), 5, 3 });

            _loggerSubstitute.Received(LogLevel.Trace, null, expectedMessage, 1041);
        }
Exemplo n.º 2
0
            public Task <bool> HandleErrorAsync(ConsumerPipelineContext context, Exception exception)
            {
                Check.NotNull(context, nameof(context));
                Check.NotNull(exception, nameof(exception));

                var nextPolicy = _policies.FirstOrDefault(policy => policy.CanHandle(context, exception));

                if (nextPolicy != null)
                {
                    return(nextPolicy.HandleErrorAsync(context, exception));
                }

                _logger.LogInboundTrace(IntegrationLogEvents.PolicyChainCompleted, context.Envelope);

                return(Task.FromResult(false));
            }
Exemplo n.º 3
0
            public Task <bool> HandleErrorAsync(ConsumerPipelineContext context, Exception exception)
            {
                Check.NotNull(context, nameof(context));
                Check.NotNull(exception, nameof(exception));

                foreach (var policy in _policies)
                {
                    if (policy.CanHandle(context, exception))
                    {
                        return(policy.HandleErrorAsync(context, exception));
                    }
                }

                _logger.LogInboundTrace(IntegrationLogEvents.PolicyChainCompleted, context.Envelope);

                return(Task.FromResult(false));
            }
Exemplo n.º 4
0
            private async Task ApplyDelayAsync(ConsumerPipelineContext context)
            {
                var delay = (int)_initialDelay.TotalMilliseconds +
                            (context.Envelope.Headers.GetValueOrDefault <int>(
                                 DefaultMessageHeaders.FailedAttempts) *
                             (int)_delayIncrement.TotalMilliseconds);

                if (delay <= 0)
                {
                    return;
                }

                _logger.LogInboundTrace(
                    IntegrationLogEvents.RetryDelayed,
                    context.Envelope,
                    () => new object?[]
                {
                    delay
                });

                await Task.Delay(delay).ConfigureAwait(false);
            }
        /// <inheritdoc cref="IErrorPolicyImplementation.CanHandle" />
        public virtual bool CanHandle(ConsumerPipelineContext context, Exception exception)
        {
            Check.NotNull(context, nameof(context));
            Check.NotNull(exception, nameof(exception));

            var failedAttempts =
                context.Envelope.Headers.GetValueOrDefault <int>(DefaultMessageHeaders.FailedAttempts);

            if (_maxFailedAttempts != null && failedAttempts > _maxFailedAttempts)
            {
                _logger.LogInboundTrace(
                    IntegrationLogEvents.PolicyMaxFailedAttemptsExceeded,
                    context.Envelope,
                    () => new object?[]
                {
                    GetType().Name,
                    failedAttempts,
                    _maxFailedAttempts
                });

                return(false);
            }

            if (_includedExceptions.Any() &&
                _includedExceptions.All(type => !type.IsInstanceOfType(exception)))
            {
                _logger.LogInboundTrace(
                    IntegrationLogEvents.PolicyExceptionNotIncluded,
                    context.Envelope,
                    () => new object?[]
                {
                    GetType().Name,
                    exception.GetType().Name
                });

                return(false);
            }

            if (_excludedExceptions.Any(type => type.IsInstanceOfType(exception)))
            {
                _logger.LogInboundTrace(
                    IntegrationLogEvents.PolicyExceptionExcluded,
                    context.Envelope,
                    () => new object?[]
                {
                    GetType().Name,
                    exception.GetType().Name
                });

                return(false);
            }

            if (_applyRule != null && !_applyRule.Invoke(context.Envelope, exception))
            {
                _logger.LogInboundTrace(
                    IntegrationLogEvents.PolicyApplyRuleReturnedFalse,
                    context.Envelope,
                    () => new object?[]
                {
                    GetType().Name
                });

                return(false);
            }

            return(true);
        }