private SinkTrace TraceSource(
            RequestHandler handler,
            HttpEventCollectorEventInfo.Metadata metadata = null,
            HttpEventCollectorSender.SendMode sendMode    = HttpEventCollectorSender.SendMode.Parallel,
            int batchInterval  = 0,
            int batchSizeBytes = 0,
            int batchSizeCount = 0,
            HttpEventCollectorSender.HttpEventCollectorMiddleware middleware = null)
        {
            var listener = new ObservableEventListener();
            var sink     = new HttpEventCollectorSink(
                uri: uri,
                token: token,
                formatter: new TestEventFormatter(),
                metadata: metadata,
                sendMode: sendMode,
                batchInterval: batchInterval,
                batchSizeBytes: batchSizeBytes,
                batchSizeCount: batchSizeCount,
                middleware: MiddlewareInterceptor(handler, middleware));

            listener.Subscribe(sink);

            var eventSource = TestEventSource.GetInstance();

            listener.EnableEvents(eventSource, EventLevel.LogAlways, Keywords.All);
            return(new SinkTrace()
            {
                Source = eventSource,
                Sink = sink,
                Listener = listener
            });
        }
        public async Task TestEventSink()
        {
            var listener = new TcpListener(IPAddress.Loopback, 0);

            listener.Start();
            int port = ((IPEndPoint)listener.Server.LocalEndPoint).Port;

            var slabListener = new ObservableEventListener();

            slabListener.Subscribe(new TcpEventSink(IPAddress.Loopback, port,
                                                    new ExponentialBackoffTcpReconnectionPolicy(),
                                                    new TestEventFormatter()));
            var source = TestEventSource.GetInstance();

            slabListener.EnableEvents(source, EventLevel.LogAlways, Keywords.All);

            var listenerClient = await listener.AcceptTcpClientAsync();

            source.Message("Boris", "Meep");

            var receiverReader = new StreamReader(listenerClient.GetStream());
            var line           = await receiverReader.ReadLineAsync();

            Assert.Equal(
                "EventId=1 EventName=MessageInfo Level=Error \"FormattedMessage=Meep - Boris\" \"message=Boris\" \"caller=Meep\"",
                line);

            listenerClient.Close();
            listener.Stop();
            slabListener.Dispose();
        }
 public static TestEventSource GetInstance()
 {
     if (instance == null)
     {
         instance = new TestEventSource();
     }
     return(instance);
 }