Skip to content

Fix issue with force-reopen after 30 minutes #337

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

Merged
merged 4 commits into from
Mar 7, 2025
Merged

Conversation

Falco20019
Copy link
Contributor

@Falco20019 Falco20019 commented Feb 20, 2025

Fixes #334

I also added a check to Emit that writes to SelfLog if the log was dropped. This could have happend also before if the file was locked 3 times or any other exception was thrown due to checking for _currentFile?.EmitOrOverflow(logEvent) == false and not _currentFile?.EmitOrOverflow(logEvent) != true but it was hard to find out that it got dropped. But since the code seems to do this intentional to avoid blocking and force-retrying after 30 minutes (or on next checkpoint), that should be correct.

I encapsulated everything into a try-finally block to ensure that this happens on ANY exception. The old code was also waiting until the next checkpoint. So if that was daily, it would drop ALL logs for the rest of the day. With my adjustment (to checking if 30 minutes is less than the next checkpoint) it will also cover the case that no rolling was setup OR that it would be longer than 30 minutes.

@Falco20019
Copy link
Contributor Author

@nblumhardt for review, thanks :)

@Falco20019
Copy link
Contributor Author

Falco20019 commented Feb 20, 2025

I also found a bug during testing with the FileSink not disposing the already-opened stream when the hook throws an exception. This lead to different test results/expectations on the different OS

On Windows this lead to the file not being accessible for the follow-up try while on Ubuntu it just worked fine. This will ensure it works reliably on both.

@Mojo12525

This comment was marked as spam.

@@ -0,0 +1,36 @@
using System.Text;

namespace Serilog.Sinks.File.Tests.Support;

This comment was marked as spam.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes no sense. Reported the user as spam bot to avoid further interaction.

Copy link
Member

@nblumhardt nblumhardt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Falco20019! Looks great 👍 Just had one minor comment.

if (_currentFile == null)
{
SelfLog.WriteLine("Log event {0} was lost since it was not possible to open the file or create a new one.", logEvent.RenderMessage());
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about doing this under the lock, since SelfLog may be writing somewhere slow. Might be safer to drop this for now, and reconsider separately.

Copy link
Contributor Author

@Falco20019 Falco20019 Feb 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do that, I just had issues already that stuff was lost and we never found out why. But will remove it to progress with the PR for now. This should really never happen on regular use-cases and therefore should be neglectable and if the file was locked, we also use the SelfLog, so it was the nearest thing to consider :)

I will just comment it out so that it's easy to add in the future.

@nblumhardt nblumhardt merged commit 5697cc1 into serilog:dev Mar 7, 2025
1 check passed
@nblumhardt
Copy link
Member

Thanks @Falco20019 😎

@Falco20019
Copy link
Contributor Author

@nblumhardt Can you please have this release as 6.0.1 before #342 will switch the version to 7.x? Due to internal policies I would otherwise not be able to use the fix until November without going through multiple additional hoops :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New file is created every 30 minutes
3 participants