Help please with Trace Class!!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Eternal
    New Member
    • Dec 2008
    • 4

    #1

    Help please with Trace Class!!

    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.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    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?

    Comment

    • mldisibio
      Recognized Expert New Member
      • Sep 2008
      • 191

      #3
      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 TextWriterTrace Listener, 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.Trunca te 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.

      Comment

      • vekipeki
        Recognized Expert New Member
        • Nov 2007
        • 229

        #4
        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 .

        Comment

        Working...