By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,953 Members | 1,109 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,953 IT Pros & Developers. It's quick & easy.

Help please with Trace Class!!

P: 4
Hello there,

I am working on an service that uses Trace.writeline(....) to log its activities. My question is, is there any way to clear this log while the application is still running. My service runs for a long time (days, if any bad code doesnt crash it) and as the time goes by the log is growin huge in size. So what I am trying to do is check for the log files' size and clear it or delete it (when it exceeds a size limit) while the service is still running. Is this possible? If not any suggestions or work arounds are welcome.
Dec 31 '08 #1
Share this Question
Share on Google+
3 Replies

Expert 5K+
P: 7,872
I went looking on msdn, and their wording suggests that you yourself have to write the actual "to disk" section of the logging, so whereever you do that, simple check the logfile size?
According to msdn it works just like Debug, just a uniform way to report messages.

Edit: on 2nd look, there is a default listener that you can examine i think.
Check Trace.Listeners[0].LogFileName for the filename maybe?
Dec 31 '08 #2

Expert 100+
P: 190
That is actually an interesting scenario. Just to let you know you are not alone, SQL Server has the same issue. If you do not manually backup the log or schedule it for periodic backups, it grows until it reaches either a pre-set limit or runs out of disk space. Either way, it simply stops and throws an error.

When you set up a TextWriterTraceListener, there are different options for creating the file. If you choose the most explicit option of actually creating the FileStream, you can set the access mode to shared read and write.
I say this because my suggestion would be to have a separate thread which periodically fires and
- checks the trace file size
- if over limit, backs it up or zips it
- truncates the existing file: FileMode.Truncate will do this...but I am not sure if it will conflict with the TraceListener's handle to the open stream.

If you can get the shared access part to work (so that both your TraceListener and the Archiving Thread can read and write to the target file) this should allow you to clean up the file while your containing application is still running. You might have to stop the TraceListener, truncate the file, then restart the TraceListener.

These are just suggestions. I haven't actually implemented a clean-up of a long-running log yet. If someone else has, hopefully they will post a clearer solution.
Dec 31 '08 #3

Expert 100+
P: 229
I don't think you will not be able to change your file while it is being used by a TraceListener.

You will probably have to remove it from Trace.Listeners, close the Listener's underlying stream, do whatever you want to do with the file, and then reopen the stream and add your Listener to Trace.Listeners.
Jan 2 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.