As I mentioned in my reply, the problem is not in your script but in the "location" in your code where you are calling your script. You're calling it within an event handler that "interrupts" the process of logging an error in order to do something else before the error gets logged. So, anything you do within that event handler will not include the error that prompted your code to execute.
To do precisely what you want requires you to export the log after the error has been written to the log. This means that you cannot use the OnLogError event handler to do so but must move that export to some other handler or some other script code.
A suggestion COULD be that, if the logging of the error stops a test, you could us "OnStopTest" event handler and place your code in there rather than in the "OnLogError" event handler. That would be my first guess, actually, as to where to move things.
I hope this helps.