Fixes several race conditions in logging which have been at the root cause of intermittent test failures. Reviewed-by: mchung, plevart