Is Trace.Close() really needed?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marc

    Is Trace.Close() really needed?

    I'm building a Windows service that writes log messages via TraceListeners.
    I assume Trace.Close() is useful for garbage collection, but I'm not sure
    where to call it in the Windows service. Any thoughts?
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Is Trace.Close() really needed?

    Marc,

    If you want to flush the contents of the trace to the file, then yes,
    you do. And no, Close is not useful for garbage collection, as the
    instances are not released.

    In your windows service, you should probably call it when the service
    shuts down, and when it pauses.

    If you call Close, and you initialized the text file trace listener with
    a filename, then it will reopen the file and write to it when another call
    to Trace is made. If you pass a stream, then the stream is closed, and it
    doesn't write to the stream again.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Marc" <Marc@discussio ns.microsoft.co mwrote in message
    news:F4AF7D66-8362-47C8-92E4-7FC95A2938D5@mi crosoft.com...
    I'm building a Windows service that writes log messages via
    TraceListeners.
    I assume Trace.Close() is useful for garbage collection, but I'm not sure
    where to call it in the Windows service. Any thoughts?

    Comment

    • Marc

      #3
      Re: Is Trace.Close() really needed?

      Thank you. I appreciate your insight. I will definitely use the file name
      approach rather than the stream approach since it sounds more flexible.

      "Nicholas Paldino [.NET/C# MVP]" wrote:
      Marc,
      >
      If you want to flush the contents of the trace to the file, then yes,
      you do. And no, Close is not useful for garbage collection, as the
      instances are not released.
      >
      In your windows service, you should probably call it when the service
      shuts down, and when it pauses.
      >
      If you call Close, and you initialized the text file trace listener with
      a filename, then it will reopen the file and write to it when another call
      to Trace is made. If you pass a stream, then the stream is closed, and it
      doesn't write to the stream again.
      >
      Hope this helps.
      >
      >
      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m
      >
      "Marc" <Marc@discussio ns.microsoft.co mwrote in message
      news:F4AF7D66-8362-47C8-92E4-7FC95A2938D5@mi crosoft.com...
      I'm building a Windows service that writes log messages via
      TraceListeners.
      I assume Trace.Close() is useful for garbage collection, but I'm not sure
      where to call it in the Windows service. Any thoughts?
      >
      >
      >

      Comment

      Working...