nreco / logging

Generic file logger for .NET Core (FileLoggerProvider) with minimal dependencies
MIT License
284 stars 56 forks source link

Logging file locked by nreco #65

Closed Demsei closed 3 months ago

Demsei commented 4 months ago

Hi, we want to collect all logging files and zip them. If the logging process is running, the logging file is locked so another process is not able to zip it. That can we do? Stopping the process is not so good. thx

VitaliyMF commented 4 months ago

How do you copy log files? You should be able to copy 'active' log file (that is currently in use by the app) if it is opened for 'read'. I tested that both on Windows and Linux.

Take a look at line 219 here where a file logger creates a FileStream:

LogFileStream = new FileStream(LogFileName, FileMode.OpenOrCreate, FileAccess.Write);

FileShare is not specified but by default it is FileShare.Read which allows other processes to open the file for 'read'.

Demsei commented 4 months ago

We are zipping with cabinet.dll from MS. The developer checks how the logging file is opened exactly. I will post the result here. thx

Demsei commented 4 months ago

Hi, I can copy active file in explorer as you mentioned. We try to open the active logfile in cabinet callback fnFileOpen with CreateFileA(fileName, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, FILE_ATTRIBUE_READONLY, nullptr). The result is INVALID_FILE_HANDLE and a call to GetLastError givs us rc=32 (ERROR_SHARING_VIOLATION). What can we do else ?

VitaliyMF commented 4 months ago

Have you tried to specify FILE_SHARE_READ|FILE_SHARE_WRITE ? It's not obvious but this sharing mode is less restrictive than just FILE_SHARE_READ (which does NOT allow other processes to write into the file, therefore you get ERROR_SHARING_VIOLATION).

Demsei commented 4 months ago

bingo! that's it !! You are me todays HERO thx