1
Vote

Flushing FileTraceListener with no logged data causes NullReferenceException

description

I think there may be a problem with the code added in changeset 61230 ("Fixed flushing problem with FileTraceListener").
 
I'm trying to use Ukadc.Diagnostics for the first time and started with this version. I need to log trace data to a file with reasonable formatting, so I decided to use FileTraceListener. I don't want to cause frequent disk writes so I'm not using trace autoflush. Given that, it seems I must manually call Flush on the FileTraceListener before the app terminates or no data will end up in the file. That's fine, I can do that.
 
The problem occurs when no data ends up being logged before Flush is called. For example, I set an EventTypeFilter to only log errors, and no errors occurred this time, and the app is shutting down. The app calls Flush on the FileTraceListener and an exception is thrown on this line:
_writerCache.FlushAllStreams();
_writerCache is null because it seems this only gets initialized once something is logged. I suppose Flush should check _writerCache or _initialised before trying to do this.

comments