title: How do you dispose a tracelistener properly?
body: <p>I've actually written a tracelistener component that logs to a networkstream. The code for that is here:</p>
  
  <pre><code>using Newtonsoft.Json.Linq;
  using System;
  using System.Collections.Generic;
  using System.IO;
  using System.Linq;
  using System.Net.Sockets;
  using System.Text;
  using System.Threading.Tasks;
  
  namespace System.Diagnostics
  {
      public class NetworkStreamWriterTraceListener : DelimitedListTraceListener
      {
          NetworkStream _stream;
          StreamWriter _writer;
          StreamReader _reader;
          TcpClient client;
          bool IsDisposed = false;
          private NetworkStream GetStream(string configJson)
          {
              JObject config = JObject.Parse(configJson);
              if (config["port"] == null)
              {
                  throw new Configuration.ConfigurationErrorsException("port missing from json configuration");
              }
              client = new TcpClient();
              int port = config["port"].Value&lt;int&gt;();
              client.Connect("localhost", port);
              return client.GetStream();
          }
  
          public NetworkStreamWriterTraceListener(string configJson): base(TextWriter.Null)
          {
              Initialize(configJson);            
          }
  
          public NetworkStreamWriterTraceListener(string configJson, string name) : base(TextWriter.Null, name)
          {
              Initialize(configJson);
          }
  
          private void Initialize(string configJson)
          {
              _stream = GetStream(configJson);
              _writer = new StreamWriter(_stream);
              _reader = new StreamReader(_stream);
              Writer = _writer;            
              _reader.ReadLine();
              //TODO: parse response code
  
              SendCommand("IDTY", configJson);
  
              _reader.ReadLine();
              //TODO: parse response code
  
              AppDomain.CurrentDomain.ProcessExit += (s, e) =&gt;
              {
                  SendCommand("QUIT", "closing connection");
              };
              AppDomain.CurrentDomain.DomainUnload += (s, e) =&gt;
              {
                  SendCommand("QUIT", "closing connection");
              };
          }
  
          public void SendCommand(string Command, string Data)
          {
              this.Writer.WriteLine("{0} {1}", Command, Data);
              this.Writer.Flush();
          }
  
          protected override void Dispose(bool disposing)
          {
              base.Dispose(disposing);
              IsDisposed = true;
          }
  
          public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string message)
          {
              Write("LMSG ");
              base.TraceEvent(eventCache, source, eventType, id, message);
              Flush();
          }
  
          public override void Close()
          {            
              base.Close();
          }
      }
  }
  </code></pre>
  
  <p>Now I've imported and consumed this tracelistener component successfully adding it via configuration.</p>
  
  <p>There is a problem in how this component is meant to be used. Before its "disposed" (i.e. in a general sense, not in the .net framework sense), its supposed to send a message downstream signalling the downstream process to quit so that the downstream process can release the associated socket connection. </p>
  
  <p>I've tried overriding Dispose method, but it seems that method never gets invoked by the framework. Tried to write a destructor routine, but whenever I try to flush the write stream, it throw an ObjectDisposedException</p>
  
  <pre><code>~NetworkStreamWriterTraceListener() 
  {
      if(client.Connected)
      {
          Send("QUIT", "done with operation");
          client.Close();
      }
  }
  </code></pre>
  
  <p>I've also tried hooking into the AppDomain events. They didn't work either.</p>