private async Task <EntityEventLog> PublishInternal(EntityEvent entityEvent)
        {
            entityEvent.CreateTime = DateTime.Now;
            var type = entityEvent.GetType();

            var            data = EntityUtilities.ToDictionary(entityEvent);
            EntityEventLog log  = new EntityEventLog
            {
                CreateTime      = DateTime.Now,
                Data            = BinaryConvert.Serialize(data),
                EntityEventType = type.FullName,
                State           = EventStateEnum.NotPublished
            };

            entityEvent.State = EventStateEnum.NotPublished;
            var binding = _messageQueue.GetBinding(type, entityEvent.Topic);

            if (binding == null)
            {
                throw new QueuePublishException($"Bindindg information not found for {type}");
            }

            log.Exchange = binding.Exchange;
            log.Topic    = binding.Topic;

            try
            {
                entityEvent.State = EventStateEnum.Published;
                log.State         = EventStateEnum.Published;

                await _messageQueue.Publish(log.Data, binding.Exchange, binding.Topic).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                entityEvent.State = EventStateEnum.NotPublished;
                log.State         = EventStateEnum.NotPublished;
                log.ErrorMessage  = ex.Message;

                _logger?.LogError(ex, $"Unable to publish event {type.Name} Exchange:{log.Exchange} Topic:{log.Topic}");
            }

            return(log);
        }
Esempio n. 2
0
        protected void Log(ActionType actionType, T entity = null)
        {
            EntityEventLog logRecord = EntityContext.EntityEventLogs.Create();

            logRecord.ActionDate = DateTime.Now;
            logRecord.ActionType = (short)actionType;
            logRecord.EntityType = typeof(T).Name;

            if (entity != null && entity.Id != 0)
            {
                logRecord.EntityJson = JsonConvert.SerializeObject(entity, Formatting.Indented,
                                                                   new JsonSerializerSettings
                {
                    ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                });
                logRecord.EntityId = entity.Id;
            }
            EntityContext.EntityEventLogs.Add(logRecord);

            EntityContext.SaveChanges();
        }