In order for BitcoinTestFramework to correctly restart after shutdown, the previous logging handlers need to be removed, or else logging will continue in the previous temp directory.
"Flush" ensures buffers are emptied, and "close"ensures file handler close logging file.
This is a backport of Core PR17288 [3/8]
https://github.com/bitcoin/bitcoin/pull/17288/commits/6f40820757d25ff1ccfdfcbdf2b45b8b65308010
Depends on D8242