您如何正确处理示踪剂? [英] How do you dispose a tracelistener properly?

查看:129
本文介绍了您如何正确处理示踪剂?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实际上已经编写了一个tracelistener组件,该组件可以记录到网络流中.的代码在这里:

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();
        }
    }
}

现在,我已经导入并使用了该tracelistener组件,并已通过配置成功添加了它.

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

该组件的使用方式存在问题.在其被部署"之前(即,从一般意义上讲,不是.net框架意义上的),它应该向下游发送一条消息,指示下游进程退出,以便下游进程可以释放关联的套接字连接.

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.

我尝试覆盖Dispose方法,但是似乎该方法从未被框架调用.试图编写一个析构函数例程,但是每当我尝试刷新写流时,它都会引发ObjectDisposedException

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();
    }
}

我也尝试过钩入AppDomain事件.他们也没有工作.

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

推荐答案

显然,ProcessExit事件起作用. :(

Apparently ProcessExit event works. :(

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

这篇关于您如何正确处理示踪剂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆