I have a Python service spitting out logs to text files. It rotates them every ~400KB. So the Python service opens up a handle on the file, let's call it app.log. It then writes content to the file every now and again flushes it to the disk. When it reaches a certain size, it closes it's handle, and move it to app.log.1 and starts a new handle on app.log.
So I can't change this service, but I have a C# application that will read those logs. I ran into 3 scenarios:
- If I just try to read those those logs using
new FileStream(path, FileMode.Open);
, it won't allow me as the Python service has an handle on it. - If I try to open it using
new FileStream(path, FileMode.Open, FileAccess.Read);
, this allows me to read it, but if the service tries to rotate the log, it won't be able to as my C# application now has a handle on the file. - And if I try to open the file using
new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Delete);
, my Python service won't fail on deleting the file, but it will fail creating a new handle on app.log as the C# application would still have a handle on it.
The only solution which I'm aware of would be using Windows Shadow Copy (VSS) to create a snapshot of the logs and then read that snapshot but this would be quite expensive as we need to query the logs at every 5 minutes.
Also, I'm not interested in reading the rotated logs, app.log.1, app.log.2 etc.
Logging to text files under Windows seems to be a pain what with all the locking/handles. Does anyone have any suggestion?