It depends on the age of the dog and other factors. We have one mature dog that still tends to do that. So we take them out a midnight and let them wander around to do their business and then we usually get to sleep all the way through.
But if the dog is older there might be some health problems. Dogs brains are not wired like ours to they do not understand in the morning if you punish them for something they did during the night.. They need to be caught in the act.
You can also kennel them at night in a particular place like a laundry area or something. Personally, our dogs are part of the family and have run of the house, but if the messing is a huge problem, you might want to consider it.