Creating sequential file log of asynchronous events

Background: Thanks to help of this community I've been able to create quite nice hobby app interfacing external devices - my friend have many models I can't test personally and it happens that those behave "slightly" different that I expected reading specs :wink: To diagnose problems I need debug file created on user devices (here is the prblem) and sent to me (this part is simple).

The problem: is log must be created sequentially as data comes-in asynchronously (from USB or BT serial) - so I can not first make a big list and write it to file at the end or later (similar to: Can't write file to SHARED directories on API31 (Android 12) - #15 by TIMAI2 but I can't apply previous solution).

Any suggestions ?

sequential as in when any event is started, or when it completes ?

I log immediatelly when : setting state, sending data to BT/USB (start), on-after-data-read from BT/USB, when polling can not fetch anything new for prolonged time (just point in code)...

(For a while, I've make a workaround being : appending to list in memory and flushing it to file on 10 sec timer - works for now at low data volumes and providing app does not fail - but it is not real unix style crash-proof log).
Regards and thanks for your interest and support (as always).

Therefore sequential as in when any event starts or finishes, or when some events have a break/check point between start and finish ?

If so, where is the issue in setting a log on each of these events ?

I just do not know how to do simple Log(textToLog) that would write textToLog into file (preferable open beforehand by call to LogOpenOrCreate (filename) ).
What I need is that text written in any call is in the file after all events Log'ed before.
If I just call AppendFile it will not always keep order.
regards, Kuba

The order or appending will (should) depend on when log events arrive. This is why I asked about this earlier

It doesn't. But leave it, my workaround (events write list, on timer writesit and 10 sec timer is long enough to keep it dido) is good enough.