Reading midi events from track via output ports in Lua

I’m currently attempting to parse the MIDI buffer for events from the track’s output ports in a session script; basically in a similar manner as the MIDI Tracer. However, when attempting to obtain a MIDIBuffer from a output port, if fails to populate with events. Does anyone have any suggestions?

Likely this is the example you are starting from, but just in case:

Thanks for the reply, but I was using the midi rewrite example. In this case I would like to check the midi events output from a track within a session script. However it seems the buffer is unable to access this, just hardware input.