您如何正确处理示踪剂?

编程入门 行业动态 更新时间:2024-10-19 10:17:18
本文介绍了您如何正确处理示踪剂?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我实际上已经编写了一个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.

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

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

更多推荐

您如何正确处理示踪剂?

本文发布于:2023-11-24 04:37:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623995.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正确处理   示踪剂

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!