Unable to write DTMF before hangup

Hi
I’m trying to read DTMF signals and then write them to a file.
it goes ok to write them to the file if asterisk is the one to end the call,
but if the application that calls in and hangs up, then nothing get written.
This is what i use:
exten => s,5,System(echo “${DATETIME} - ${CALLERID} - {CHANNEL} - ${Secret}” >> /var/log/asterisk/calls)
exten => s,6,Wait(5)
exten => t,7,Hangup

any ideas on how to get them written to file before hangup.?