Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug - Dispose() doesn't wait for messages to be sent to the database before terminating resulting in possible log losses #24

Open
fbedogni opened this issue Mar 17, 2024 · 0 comments · Fixed by fbedogni/serilog-sinks-mysql#1 · May be fixed by #25

Comments

@fbedogni
Copy link

Problem
The application terminates before the sink has finished uploading the logs to the database.

To reproduce
Create a small console app in which you create a logger, log something, dispose the logger and exit.
Most of the time the application will terminate almost istantly and the log won't be written to the database.

Source of the bug
In the constructor of the class BatchProvider the task _batchTask, which is responsible for sending logs, is initialized using the Task.Factory.StartNew
image
This task should be completed only when all the logs have been written to the database, but it's not the case.
This causes the Task.WaitAll in the Dispose() method to return before all the logs have been written to the database
To make sure that _batchTask waits for all the internal tasks to finish you need to call the Unwrap() method.
This SO explains it better than i can task-factory-startnew-wont-wait-for-task-completion

fbedogni added a commit to fbedogni/serilog-sinks-mysql that referenced this issue Mar 17, 2024
… for the batch thread to finish work before completing.

Resolves saleem-mirza#24
@fbedogni fbedogni linked a pull request Mar 17, 2024 that will close this issue
@fbedogni fbedogni reopened this Mar 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant