The district court of Zofingen handed the sentence to the 30-year-old refugee on Thursday after he admitted to killing the victim one evening in April 2012, the SDA news agency reported.
The defendant explained that the Swiss came to talk to him at an asylum centre in Brittnau, where he promised him a job with a farmer, according to court testimony.
He invited the Moroccan to his home, where the pair drank beer before going upstairs to a bedroom and the Moroccan claimed he fell asleep on a bed, the SDA said.
The refugee testified he felt he needed to defend himself after the Swiss tried to sexually attack him.
The court earlier heard evidence that the Moroccan tied the hands and feet of the older man before beating him with the log in order to steal some personal possessions.
During the court case, witnesses testified that the victim had often visited the asylum centre, where he sought out men for sexual relations, the SDA said.
Investigators found traces of DNA from two other refugees in the Swiss man’s bed.
The prosecutor had nonetheless sought a life sentence against the Moroccan, who entered Switzerland illegally in February 2012.
The man already had a criminal record in Switzerland after stealing a handbag from a woman in Zofingen.
For that offence he was ordered to pay a financial penalty, as well as a fine.