private void Save(LocalChannelLogEntry entry)
 {
     lock (_syncObject)
     {
         _localPersistenceContext.Add(entry);
         _localPersistenceContext.SaveChanges();
     }
 }
        private void LogChannelMessage(LocalChannel channel, string temporaryChannelId, Message message,
                                       LocalChannelLogEntryType type)
        {
            var entry = new LocalChannelLogEntry
            {
                ChannelId          = channel != null && channel.Id != 0 ? channel.Id : (int?)null,
                Timestamp          = DateTime.Now,
                EntryType          = type,
                TemporaryChannelId = temporaryChannelId,
                State          = channel?.State,
                AdditionalData = message.ToString()
            };

            Save(entry);
        }
        public void LogPendingChannelInfo(string temporaryChannelId, LocalChannelState state, string logText = "")
        {
            var entry = new LocalChannelLogEntry
            {
                Timestamp          = DateTime.Now,
                EntryType          = LocalChannelLogEntryType.Info,
                TemporaryChannelId = temporaryChannelId,
                State     = state,
                LogText   = logText,
                DebugData = GetDebugData()
            };

            _logger.LogInformation($"{logText}. State: {state}. TemporaryChannelID: {temporaryChannelId}");
            Save(entry);
        }
        private void LogMessageValidationException(LocalChannel channel, string temporaryChannelId, Message message,
                                                   LocalChannelLogEntryType type, string errorText)
        {
            var entry = new LocalChannelLogEntry
            {
                ChannelId          = channel != null && channel.Id != 0 ? channel.Id : (int?)null,
                Timestamp          = DateTime.Now,
                EntryType          = type,
                TemporaryChannelId = temporaryChannelId,
                State          = channel?.State,
                Error          = LocalChannelError.ValidationError,
                ErrorText      = errorText,
                AdditionalData = message.ToString()
            };

            Save(entry);
        }
        public void LogPendingChannelError(string temporaryChannelId, LocalChannelState state, LocalChannelError error,
                                           string errorText)
        {
            var entry = new LocalChannelLogEntry
            {
                Timestamp          = DateTime.Now,
                EntryType          = LocalChannelLogEntryType.Error,
                TemporaryChannelId = temporaryChannelId,
                State     = state,
                Error     = error,
                ErrorText = errorText,
                DebugData = GetDebugData()
            };

            _logger.LogError($"{error}: {errorText}. TemporaryChannelID: {temporaryChannelId}");
            Save(entry);
        }
        public void Log(LocalChannel channel, string logText, LocalChannelLogEntryType type, string additionalData = "")
        {
            var entry = new LocalChannelLogEntry
            {
                Timestamp          = DateTime.Now,
                EntryType          = type,
                ChannelId          = channel != null && channel.Id != 0 ? channel.Id : (int?)null,
                TemporaryChannelId = channel?.TemporaryChannelId,
                ChannelData        = channel?.ToString(),
                State          = channel?.State,
                LogText        = logText,
                DebugData      = GetDebugData(),
                AdditionalData = additionalData
            };

            string channelData = channel != null?channel.ToString() : "";

            _logger.LogDebug($"{type}: {logText}. AdditionalData: {additionalData}. Channel: {channelData}");
            Save(entry);
        }
        public void LogError(LocalChannel channel, LocalChannelError error, string errorText, string additionalData = "")
        {
            var entry = new LocalChannelLogEntry
            {
                Timestamp          = DateTime.Now,
                EntryType          = LocalChannelLogEntryType.Error,
                ChannelId          = channel != null && channel.Id != 0 ? channel.Id : (int?)null,
                TemporaryChannelId = channel?.TemporaryChannelId,
                ChannelData        = channel?.ToString(),
                State          = channel?.State,
                Error          = error,
                ErrorText      = errorText,
                DebugData      = GetDebugData(),
                AdditionalData = additionalData
            };

            string channelData = channel != null?channel.ToString() : "";

            _logger.LogError($"{error}: {errorText}. AdditionalData: {additionalData}. Channel: {channelData}");
            Save(entry);
        }
        public void LogStateUpdate(LocalChannel channel, LocalChannelState oldState, string logText = "", string additionalData = "")
        {
            var entry = new LocalChannelLogEntry
            {
                Timestamp          = DateTime.Now,
                EntryType          = LocalChannelLogEntryType.StateUpdate,
                ChannelId          = channel != null && channel.Id != 0 ? channel.Id : (int?)null,
                TemporaryChannelId = channel?.TemporaryChannelId,
                ChannelData        = channel?.ToString(),
                State          = channel?.State,
                OldState       = oldState,
                LogText        = logText,
                DebugData      = GetDebugData(),
                AdditionalData = additionalData
            };

            string channelData = channel != null?channel.ToString() : "";

            string newState = channel != null?channel.State.ToString() : "";

            _logger.LogDebug($"Channel state update. New state: {newState}. Old state: {oldState}. {logText}. " +
                             $"AdditionalData: {additionalData}. Channel: {channelData}");
            Save(entry);
        }