Excitation of Be+ and Mg+ in collisions with rare gases: measurements of Be II and Mg II resonance line emission and polarization