I'm not so sure - but very happy to be put right.
Extreme case, if I steal a plane and fly off with it - as long as I'm type rated etc. surely I can log the time as that part of the regulations is complied with?
Some of the earlier answers suggest that every single aspect of a flight must be legal for the time to be legal - and whilst clearly a good idea (!) I'm not sure it's needed for putting hours in your logbook.