Non-locking TextWriterTraceListener?
I'm using TextWriterTraceListener
to ouptput my trace information to a log file开发者_如何学运维. Unfortunately it locks the logfile and I am not able to open it externally while the application is running. Any way to make this possible?
It depends what constructor you used. The TextWriterTraceListener(String) constructor creates a StreamWriter that opens the file with FileShare.Read. That allows any process to read the file.
The usual problem is trying to open the file with the wrong FileShare setting in the other process. You have to specify FileShare.ReadWrite. The trace listener has already gained write access to the file, you cannot deny it.
Whether you can monitor the file externally or not when using TextWriterTraceListener
depends a bit on what software you use for monitoring it. I usually use BareTail that has no issues with the file being locked.
I actually wrote one very recently. Unfortunately I'm not able to share the source code BUT I can tell you that there's less than 500 lines of code. The key is opening the file correctly, allowing read sharing as you've noted. The other thing I did was buffered the writes and only wrote them after I received a set number of bytes. I also wrote asynchronously so it didn't block the application.
精彩评论