Flies are attracted to light because they naturally use the light reflected off the moon to guide them. Using the light of the moon, a fly can keep a straight flight direction by keeping the light at a steady angle.
Flies and many other insects are disturbed by artificial lights. Lamps and light bulbs in particular can disorient insects and leave them vulnerable to predators. When an insect gets within close range of an artificial light source, it attempts to keep a straight flying path just as it does with the moonlight. Since bulbs and lamps emit light in a 360-degree range, this leaves the insect flying around in circles.