/// <summary>
        /// Handles an error message by saving it in the erro rdatabase/table.
        /// </summary>
        /// <param name="message">The Rabbit error message.</param>
        /// <returns>The identoifier of the saved error. In this case the record number in the table.</returns>
        public Task HandleDefaultErrorMessage(IMessage <RabbitErrorMessage> message)
        {
            var task = Task.Run(
                () =>
            {
                try
                {
                    var errorId = _store.Save(message.Body);
                    _logger.Debug($"Saved error for correlation id {message.Body.CorrelationId} with error id {errorId}.");
                }
                catch (Exception ex)
                {
                    _logger.Error(ex, "Error trying to save exception in store: [{0}], trying to use the fallback store: [{1}]", _store.GetType().Name, _fallbackStore.GetType().Name);

                    try
                    {
                        var errorId = _fallbackStore.Save(message.Body);
                        _logger.Debug($"Saved in the fallback error store error for correlation id {message.Body.CorrelationId} with error id {errorId}.");
                    }
                    catch (Exception ex2)
                    {
                        _logger.Error(ex2, "Error trying to save exception in the fallbackstore of type: [{0}], will silently continue without saving. The original exception message was: [{1}]", _fallbackStore.GetType().Name, JsonConvert.SerializeObject(message.Body));
                    }
                }
            });

            return(task);
        }
Exemplo n.º 2
0
        internal static void ProcessSavedFiles <T>(ILogger logger, ILogStore <T> store, string folder)
        {
            if (!Directory.Exists(folder))
            {
                return;
            }

            logger.Info("Looking for previously saved files to store in the management database.");

            // See if we previously saved fles to disk, so we can now load them up into the database
            foreach (var file in Directory.EnumerateFiles(folder))
            {
                var message = JsonConvert.DeserializeObject <T>(Encoding.UTF8.GetString(File.ReadAllBytes(file)));

                try
                {
                    store.Save(message);
                }
                catch (Exception ex)
                {
                    logger.Error(ex, "Error loading audit record from file [{0}] into the database, leaving file intact.", file);
                }
                finally
                {
                    File.Delete(file);

                    logger.Debug("Saved audit record from file: [{0}] to the database and removed it from disk.", file);
                }
            }
        }
        /// <summary>
        /// Handles autit messages from for the current handler.
        /// </summary>
        /// <param name="message">The audit message.</param>
        /// <returns>An asyncrounous task.</returns>
        public Task HandleAuditMessage(IMessage <RabbitAuditMessage> message)
        {
            var task = Task.Run(
                () =>
            {
                try
                {
                    _store.Save(message.Body);
                }
                catch (Exception ex)
                {
                    _logger.Error(ex, "Error trying to save audit record in store: [{0}], trying to use the fallback store: [{1}]", _store.GetType().Name, _fallbackStore.GetType().Name);

                    try
                    {
                        _fallbackStore.Save(message.Body);
                    }
                    catch (Exception ex2)
                    {
                        _logger.Error(ex2, "Error trying to save audit record in the fallbackstore of type: [{0}], will silently continue without saving. The original exception message was: [{1}]", _fallbackStore.GetType().Name, JsonConvert.SerializeObject(message.Body));
                    }
                }
            });

            return(task);
        }