private async Task ProcessEvent(string eventName, string message)
        {
            _logger.LogTrace("Processing RabbitMQ event: {eventName}", eventName);

            var messageType = _messageHandlerManager.MessageHandlerWrappers
                              .FirstOrDefault(p => p.MessageName == eventName)
                              ?.MessageType;

            if (messageType != null)
            {
                var integrationEvent = (IMessage)JsonConvert.DeserializeObject(message, messageType);

                _logger.LogTrace("Enable diagnostic listeners before consume,name is {name}", DiagnosticListenerConstants.BeforeConsume);
                EventBusDiagnosticListener.TracingConsumeBefore(integrationEvent);

                var messageHandlers = _messageHandlerProvider.GetHandlers(messageType);
                foreach (var messageHandler in messageHandlers)
                {
                    var concreteType = typeof(IMessageHandler <>).MakeGenericType(messageType);
                    var method       = concreteType.GetMethod("HandAsync");
                    if (method == null)
                    {
                        continue;
                    }
                    try
                    {
                        await(Task) method.Invoke(messageHandler, new object[] { integrationEvent });
                    }
                    catch (Exception e)
                    {
                        var handlerType = messageHandler.GetType();
                        _logger.LogError("Message processing failure,message type is {messageType},handler type is {handlerType},error message is {errorMessage}",
                                         messageType, handlerType, e.Message);

                        _logger.LogTrace("Enable diagnostic listeners incorrect consume,name is {name}", DiagnosticListenerConstants.ErrorConsume);
                        EventBusDiagnosticListener.TracingConsumeError(integrationEvent, handlerType, e.Message);
                    }
                }

                _logger.LogTrace("Enable diagnostic listeners after consume,name is {name}", DiagnosticListenerConstants.AfterConsume);
                EventBusDiagnosticListener.TracingConsumeAfter(integrationEvent);
            }
            else
            {
                _logger.LogWarning("No subscription for RabbitMQ event: {eventName}", eventName);

                _logger.LogTrace("Not subscribed to enable diagnostic listener,name is {name}", DiagnosticListenerConstants.NotSubscribed);
                EventBusDiagnosticListener.TracingNotSubscribed(message);
            }
        }
Exemplo n.º 2
0
        public override async Task SendAsync <T>(T message)
        {
            var messageHandlers = _messageHandlerProvider
                                  .GetHandlers <T>()
                                  .ToList();

            if (messageHandlers.Any())
            {
                _logger.LogTrace("Enable diagnostic listeners before consume,name is {name}", DiagnosticListenerConstants.BeforeConsume);
                EventBusDiagnosticListener.TracingConsumeBefore(message);

                foreach (var messageHandler in messageHandlers)
                {
                    var concreteType = typeof(IMessageHandler <>).MakeGenericType(typeof(T));
                    var method       = concreteType.GetMethod("HandAsync");
                    if (method == null)
                    {
                        continue;
                    }
                    try
                    {
                        await(Task) method.Invoke(messageHandler, new object[] { message });
                    }
                    catch (Exception e)
                    {
                        var handlerType = messageHandler.GetType();
                        var messageType = message.GetType();
                        _logger.LogError("Message processing failure,message type is {messageType},handler type is {handlerType},error message is {errorMessage}",
                                         messageType, handlerType, e.Message);

                        _logger.LogTrace("Enable diagnostic listeners incorrect consume,name is {name}", DiagnosticListenerConstants.ErrorConsume);
                        EventBusDiagnosticListener.TracingConsumeError(message, handlerType, e.Message);
                    }
                }

                _logger.LogTrace("Enable diagnostic listeners after consume,name is {name}", DiagnosticListenerConstants.AfterConsume);
                EventBusDiagnosticListener.TracingConsumeAfter(message);
            }
            else
            {
                var messageName = MessageNameAttribute.GetNameOrDefault(message.GetType());
                _logger.LogWarning("No subscription for local memory message: {eventName}", messageName);

                _logger.LogTrace("Not subscribed to enable diagnostic listener,name is {name}", DiagnosticListenerConstants.NotSubscribed);
                EventBusDiagnosticListener.TracingNotSubscribed(message);
            }
            await Task.CompletedTask;
        }