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

Request add: FlushWithoutDispose #88

Open
huangjinshe opened this issue Sep 4, 2023 · 4 comments
Open

Request add: FlushWithoutDispose #88

huangjinshe opened this issue Sep 4, 2023 · 4 comments

Comments

@huangjinshe
Copy link

huangjinshe commented Sep 4, 2023

I need to read the log file, but continue logging to file, could we add something like FlushWithoutDispose?

Another problem I have is : does it using shared lock? I set WriteTo.Async(a => a.File(Common.Instance.Configs.LogPath, rollingInterval: RollingInterval.Day, shared: true)) but when I use

Log.Warning("testtesttesttesttesttesttesttesttesttesttest");
await Task.Delay(3000);

File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read)

it will still throw exception:

The process cannot access the file 'C:\log.txt' because it is being used by another process.

@nblumhardt
Copy link
Member

Hi! The problem in your example is that you need to try reopening the file with a very permissive FileShare value (at least read and write). May be a good one to debug over on Stack Overflow, if you need some more info. HTH!

@huangjinshe
Copy link
Author

Hi! The problem in your example is that you need to try reopening the file with a very permissive FileShare value (at least read and write). May be a good one to debug over on Stack Overflow, if you need some more info. HTH!

You right, I could open the file without exception by using FileShare.ReadWrite, but I don't need to write anything, I just need to read it, could you please explain it?? thanks.

Also I still think we should add something like FlushWithoutDispose, which may let some async content write to log before we using the log file and no affact to continue logging.

@nblumhardt
Copy link
Member

👍 FileShare specifies what other users of the file can do with it while you have it open.

@nblumhardt
Copy link
Member

I don't think FlushWithoutDispose stands a good chance of being implemented at this point; if you're really keen on this, you can use ReloadableLogger from Serilog.Extensions.Hosting:

https://github.com/serilog/serilog-extensions-hosting/blob/dev/src/Serilog.Extensions.Hosting/Extensions/Hosting/ReloadableLogger.cs

You can flush the logger by calling Reload() on it. There could be some subtleties to it, but it's pretty close to what you want.

HTH!

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

No branches or pull requests

2 participants