Exemplo n.º 1
0
        public virtual Task OnSubscriberExceptionAsync(IRawConsumer consumer, SubscriptionConfiguration config, BasicDeliverEventArgs args, Exception exception)
        {
            if (!config.NoAck)
            {
                consumer.Model.BasicAck(args.DeliveryTag, false);
                consumer.AcknowledgedTags.Add(args.DeliveryTag);
            }
            try
            {
                _logger.LogDebug($"Attempting to publish message '{args.BasicProperties.MessageId}' to error exchange.");

                var topologyTask = _topologyProvider.DeclareExchangeAsync(_errorExchangeCfg);
                var channelTask = _channelFactory.GetChannelAsync();
                return Task
                    .WhenAll(topologyTask, channelTask)
                    .ContinueWith(t =>
                    {
                        var msg = _serializer.Deserialize(args);
                        var errorMsg = new HandlerExceptionMessage
                        {
                            Exception = exception,
                            Time = DateTime.Now,
                            Host = Environment.MachineName,
                            Message = msg,
                        };
                        consumer.Model.BasicPublish(
                            exchange: _errorExchangeCfg.ExchangeName,
                            routingKey: args.RoutingKey,
                            basicProperties: args.BasicProperties,
                            body: _serializer.Serialize(errorMsg)
                            );
                    });
            }
            catch (Exception e)
            {
                _logger.LogWarning($"Unable to publish message '{args.BasicProperties.MessageId}' to default error exchange.", e);
                return Task.FromResult(true);
            }
        }
Exemplo n.º 2
0
		public virtual async Task OnSubscriberExceptionAsync(IRawConsumer consumer, SubscriptionConfiguration config, BasicDeliverEventArgs args, Exception exception)
		{
			if (!config.NoAck)
			{
				consumer.Model.BasicAck(args.DeliveryTag, false);
				consumer.AcknowledgedTags.Add(args.DeliveryTag);
			}
			try
			{
				_logger.LogError($"Error thrown in Subscriber: ", exception);
				_logger.LogDebug($"Attempting to publish message '{args.BasicProperties.MessageId}' to error exchange.");

				await _topologyProvider.DeclareExchangeAsync(_errorExchangeCfg);
				var channel = await _channelFactory.GetChannelAsync();
				var msg = _serializer.Deserialize(args);
				var actualException = UnwrapInnerException(exception);
				var errorMsg = new HandlerExceptionMessage
				{
					Exception = new ExceptionInformation
					{
						ExceptionType = actualException.GetType().FullName,
						InnerMessage = actualException.InnerException?.Message,
						Message = actualException.Message,
						StackTrace = actualException.StackTrace
					},
					Time = DateTime.Now,
					Host = Environment.MachineName,
					Message = msg,
				};
				channel.BasicPublish(
					exchange: _errorExchangeCfg.ExchangeName,
					routingKey: args.RoutingKey,
					basicProperties: args.BasicProperties,
					body: _serializer.Serialize(errorMsg)
					);
				channel.Close();
			}
			catch (Exception e)
			{
				_logger.LogWarning($"Unable to publish message '{args.BasicProperties.MessageId}' to default error exchange.", e);
			}
		}