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

Truncating/Deleting log files while logging application is running #78

Open
damien-sorin opened this issue Dec 10, 2024 · 4 comments
Open
Labels

Comments

@damien-sorin
Copy link

Hi,

within our windows application there is a debug logging mode which generates quite a lot of log output.

If this mode is active it would be nice to occasionally truncate or delete the log file(s) without stopping and restarting application.

This would be very helpful when "debugging" the application on systems of the application user (without development environment).

Would it be possible to implement a configuration key like "non-exclusive" for the file logger, as the FileShareMode is currently hard coded as FileShare.Read without sharing write and delete operations.

For compatibility reason this option could be defaulting to the current behavior, of course.

@VitaliyMF
Copy link
Contributor

If this mode is active it would be nice to occasionally truncate or delete the log file(s) without stopping and restarting application.

For this purpose you can use rolling file feature, it has several modes so you can choose which one is more suitable exactly for your app.

Would it be possible to implement a configuration key like "non-exclusive" for the file logger, as the FileShareMode is currently hard coded as FileShare.Read without sharing write and delete operations.

FileShare.Read allows other processes to open active log file for 'read'; it is a bit unclear how log file can work in FileShare.ReadWrite mode, most likely you'll not able to achieve what you want by opening active log file for 'write' (and do what?..).

@damien-sorin
Copy link
Author

damien-sorin commented Dec 13, 2024

Hi, thanks for your quick answer!

For this purpose you can use rolling file feature, it has several modes so you can choose which one is more suitable exactly for your app.

We know about the rolling feature - sorry for this misunderstanding.

The use case is not to roll the log based on time or file size, but to truncate it to size 0 on an event manually observed while using the application and running into an error on a live system.

for example with this command line

copy /Y nul: logfile.log

or by some code in our application of course.

FileShare.Read allows other processes to open active log file for 'read'; it is a bit unclear how log file can work in FileShare.ReadWrite mode, most likely you'll not able to achieve what you want by opening active log file for 'write' (and do what?..).

Yeah, I know what you mean. But if you know what you are doing an opt-in configuration would enable it if you need it. To be honest, I have seen many loggers not restricting concurrent write or delete operations (or probably just don't keep the file open while not writing into it) without any problems.

It would help devops to do service for their customers ;-)

@VitaliyMF
Copy link
Contributor

But if you know what you are doing an opt-in configuration would enable it if you need it. To be honest, I have seen many loggers not restricting concurrent write or delete operations without any problems.

If you need an option that changes FileShare for opened log file it can be added, no problems with that.

However, I want to ensure that this option really is needed and you can achieve what you need - could you confirm that? You can simply change it here https://github.com/nreco/logging/blob/master/src/NReco.Logging.File/FileLoggerProvider.cs#L220 and try your build to check that.

@damien-sorin
Copy link
Author

damien-sorin commented Dec 13, 2024

good idea, we will check that and give you some feedback

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

No branches or pull requests

2 participants