I have a .NET console application that uses the official Rollbar .NET API to post log messages to Rollbar asynchronously. The C# code used to send each message looks like:
RollbarLocator.RollbarInstance.Log(ErrorLevel.Error, "test message");
I've noticed that if my application terminates shortly after an async message is sent, that message often won't make it to Rollbar -- evidently because the message is still in a pending state at the time of termination.
All pending messages generally will get sent successfully if I have my application sleep for several seconds just before exiting:
// Give outgoing async Rollbar messages time to send before we exit.
System.Threading.Thread.Sleep(6000);
However, obviously, that approach is not terribly elegant.
The docs do briefly touch on this situation:
However, in some specific situations (such as while logging right before exiting an application), you may want to use a logger fully synchronously so that the application does not quit before the logging completes (including subsequent delivery of the corresponding payload to the Rollbar API).
In my scenario, however, I don't necessarily know whether the program is about to exit at the time that I am logging a given message.
I could log a debug-level "Exiting now!" message synchronously just before the program terminates; however, it's not clear from the documentation whether or not doing so causes any pending async messages to also be sent?
Is there an elegant way to guarantee that all pending async messages sent to Rollbar actually have been sent (or have timed out) before my program terminates?
You need to use a blocking logger in this case. For example:
For more details, please, look here: https://docs.rollbar.com/docs/basic-usage#blocking-vs-non-blocking-logging