protected TResult ExecuteQuery <TResult>(Func <NpgsqlCommand, TResult> func) { var sql = string.Empty; NpgsqlParameterCollection sqlParams = null; var result = default(TResult); using (var connection = new NpgsqlConnection(_connectionString)) { try { connection.Open(); using (var command = connection.CreateCommand()) { sql = command.CommandText; sqlParams = command.Parameters; command.CommandType = CommandType.Text; result = func(command); } } catch (NpgsqlException e) { var paramMessage = (sqlParams != null) ? $"Параметры {string.Join(',', sqlParams.Select(x => x.Value.ToString().ToArray()))}" : string.Empty; _logger.LogError(e, $"Ошибка при выполнении запроса в базу. " + $"Текст запроса:\n {sql}\n {paramMessage}"); } finally { connection.Close(); } } return(result); }