I'm using latest Business Central SAAS 23, trying to add in-database tracing for some long running processes. (I can definitely do something via outbound HTTP, but it'd be more convenient and probably faster if the traces were rows in a custom BC table).
The issue I'm having is that I can't isolate the database transaction for the logging from whatever implicit transaction is underway in the main extension code. I'd like to be able to simply insert debug/trace records as the code progresses, but have those records remain if the calling procedure's transaction is rolled back because of some database transaction error (primary key, can't obtain lock, etc).
I've tried:
If this tracing approach doesn't work out I guess I can just buffer the log lines in memory until the next Commit and then write them out, but again that's fairly inconvenient.
Is it actually possible to maintain a separated database transaction for e.g. logging?
Found this article "Out-of-transaction database writes" that does exactly what I was looking for. https://vjeko.com/2015/11/20/out-of-transaction-database-writes/
Seems it does work with a 2nd session, so I'm not sure what I was doing wrong with my prior attempts.
The attached link contains code that spawns a session for each call to the logging function (which is possibly a bit slow); that could be improved with some coordination from the calling code.