4
Answers

Trace class as log file

Jayakrishna

Jayakrishna

17y
3.7k
1

Hi, I'm building a windows service in C# which performs a file watch on a specific folder and spawns a thread for each found file for it's processing.  The problem I encountered was that all threads (working on different individual files) write data exception problems with file into a common log file.  The log file is a plain text file and it is opened using StreamWriter and closed immediately after writing to it.

When I drop 20+ files in the file watch folder at the same time, the service spawns 20+ threads and somewhere down the line, I get an exception for log file write as it is being used by the other thread.

So I ended up leveraging the built-in Trace class to do the logging to a text file by adding a listener component.  Now after dropping 50 or 100 files, the log file never gets contention error and everything looks perfect.

Now here is my question to C# experts:  Is it ok to use Trace class for logging the application data problem even though it is designed for debugging purposes? Any performance issues, etc?

Thanks.

Answers (4)