views:

233

answers:

2

I'm using .NET's built-in Tracing mechanism to trace program execution. The problem is the trace file can grow to several MB very quickly. Is there an efficient way of truncating the file when it grows past a certain point? I thought of putting in a check whenever something gets written (I have a custom class that inherits from TraceListener) but that might degrade performance too much. Maybe a Timer would be a better solution?

Anyone have any ideas?

+1  A: 

Perhaps use an Event Log trace listener, since the EventLog can be easily automatically cycled in this way.

Joel Coehoorn
I already log information to the Event log, but it doesn't contain Trace Information. I only enable tracing during Debug or QA sessions.
ilitirit
+4  A: 

Implement a custom trace listener:

Let's see how we can solve fairly common task: managing continuously generated trace files. Default trace listener implementation is not really suitable for service applications that are supposed to be always active. If application produces a lot of trace output, then sooner or later this information will use up all disk space.

http://www.codeproject.com/KB/dotnet/customnettracelisteners.aspx?display=Print

With this example, you are able to create log chunck like this:

MyTrace.txt (recent trace information); 
MyTrace00.txt (trace history backup); 
MyTrace01.txt (trace history backup); 
and so on...

Of course, you can throw backup files away.

(Just a hint: try log4net (here) and Enterprise Library Logging Block (here)!)

boj
The reason I'm using the built-in tracing is because I don't need big logging solution. I just need to be able to trace during debugging sessions
ilitirit