Non-blocking TextWriterTraceListener?

I use TextWriterTraceListener to output my trace information to a log file. Unfortunately, it locks the log file, and I cannot open it from the outside while the application is running. Any way to make this possible?

+4
source share
3 answers

It depends on which constructor you used. The TextWriterTraceListener (String) constructor creates a StreamWriter that opens the file using FileShare.Read. This allows any process to read the file.

A common problem is trying to open a file with the wrong FileShare option in another process. You must specify FileShare.ReadWrite. The trace listener has already gained write access to the file; you cannot deny it.

+4
source

Regardless of whether you can control the file from the outside or not, when using TextWriterTraceListener it depends a little on what software you use to monitor it. Usually I use BareTail , which has no problems with the locked file.

+2
source

I actually wrote it recently. Unfortunately, I cannot share the source code, but I can say that there are less than 500 lines of code. The key opens the file correctly, allowing you to read access, as you noted. Another thing I did was buffer the records and only write them after I received a certain number of bytes. I also wrote asynchronously, so it did not block the application.

0
source

Source: https://habr.com/ru/post/1301258/


All Articles