TextWriterTraceListener on background thread
I've got a 3rd party component which uses the TraceSwitch functionality to allow me to output some traces of what's going on inside. Unfortunately, running the switches in verbose mode, with a TextWriterTraceListener as the consumer (outputs to file) slows down the application too much.
It isn't critical that the traced data is written immediately, so is there a way to get the data written on a lower priority thread? Perhaps a Task?
EDIT
Upon further investi开发者_如何学运维gation, it seems merely turning on the switches without attaching the listener causes the slowdown. I'm going to get a hold of the component provider.
Would still be interesting to hear an answer though.
Write your own extension to the TraceListener. In the extension, put all the trace strings onto a List<string> and when the count gets high enough, write the list out to a file and clear the list to start again. Flush the list on Dispose().
This can then be easily extended to use the Thread Pool to Queue a new task do the actual write.
This doesn't guarantee that you will improve performance. It will only help if you are sure it is the IO that is slowing things down.
精彩评论