简体   繁体   中英

How do you dispose a tracelistener properly?

I've actually written a tracelistener component that logs to a networkstream. The code for that is here:

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<int>();
            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) =>
            {
                SendCommand("QUIT", "closing connection");
            };
            AppDomain.CurrentDomain.DomainUnload += (s, e) =>
            {
                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();
        }
    }
}

Now I've imported and consumed this tracelistener component successfully adding it via configuration.

There is a problem in how this component is meant to be used. Before its "disposed" (ie 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.

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

~NetworkStreamWriterTraceListener() 
{
    if(client.Connected)
    {
        Send("QUIT", "done with operation");
        client.Close();
    }
}

I've also tried hooking into the AppDomain events. They didn't work either.

Apparently ProcessExit event works. :(

        AppDomain.CurrentDomain.ProcessExit += (s, e) =>
        {
            SendCommand("QUIT", "closing connection");
            _reader.ReadLine();
            //TODO : verify the response code received
            client.Close();
        };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM