public void TestCPUUsage()
        {
            var cpuCounter = new PerformanceCounter("Process", "% Processor Time", Process.GetCurrentProcess().ProcessName);

            cpuCounter.NextValue();

            Thread.Sleep(TimeSpan.FromSeconds(2));

            // Read inital CPU usage
            var startProcessorPercent = cpuCounter.NextValue();

            // Setup writer

            var listener = new TcpListener(IPAddress.Loopback, 0);

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

            var policy = new TriggeredTcpReconnectionPolicy();

            policy.Trigger();
            using (var writer = new TcpSocketWriter(IPAddress.Loopback, port, policy, 2))
            {
                Thread.Sleep(TimeSpan.FromSeconds(2));

                // Read new CPU usage value
                var newProcessorPercent = cpuCounter.NextValue();

                // Assert difference in CPU usage
                Assert.InRange(newProcessorPercent - startProcessorPercent, -5, 5);
            }
        }
        public void TestReconnectFailure()
        {
            var listener = new TcpListener(IPAddress.Loopback, 0);

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

            var writer   = new TcpSocketWriter(IPAddress.Loopback, port, new TryOnceTcpConnectionPolicy(), 2);
            var receiver = listener.AcceptTcpClient();

            receiver.Close();

            var errors      = new List <Exception>();
            var errorThrown = false;

            writer.LoggingFailureHandler += (ex) => {
                errorThrown = true;
                errors.Add(ex);
            };
            listener.Stop();

            while (!errorThrown)
            {
                writer.Enqueue("boris\r\n");
            }
            writer.Dispose();

            Assert.Equal(3, errors.Count());
            Assert.True(errors[0] is SocketException);
            Assert.True(errors[1] is SocketException);
            Assert.True(errors[2] is TcpReconnectFailureException);
        }
Beispiel #3
0
        /// <summary>
        /// Creates an instance of the Splunk TCP Sink
        /// </summary>
        /// <param name="hostAddress">The Splunk Host</param>
        /// <param name="port">The UDP port configured in Splunk</param>
        /// <param name="formatProvider">Optional format provider</param>
        public SplunkViaTcpSink(
            IPAddress hostAddress,
            int port,
            IFormatProvider formatProvider = null)
        {
            var reconnectionPolicy = new ExponentialBackoffTcpReconnectionPolicy();

            _writer = new TcpSocketWriter(hostAddress, port, reconnectionPolicy, 10000);

            _jsonFormatter = new JsonFormatter(renderMessage: true, formatProvider: formatProvider);
        }
        /// <summary>
        /// Creates an instance of the Splunk TCP Sink
        /// </summary>
        /// <param name="hostAddress">The Splunk Host</param>
        /// <param name="port">The UDP port configured in Splunk</param>
        /// <param name="formatProvider">Optional format provider</param>
        /// <param name="renderTemplate">If true, the message template will be rendered</param>
        public TcpSink(
            IPAddress hostAddress,
            int port,
            IFormatProvider formatProvider = null,
            bool renderTemplate = true)
        {
            var reconnectionPolicy = new ExponentialBackoffTcpReconnectionPolicy();

            _writer = new TcpSocketWriter(hostAddress, port, reconnectionPolicy, 10000);

            _jsonFormatter = new SplunkJsonFormatter(renderMessage: true, formatProvider: formatProvider, renderTemplate: renderTemplate);
        }
        public async Task TestEventsQueuedCanBeDropped()
        {
            var listener = new TcpListener(IPAddress.Loopback, 0);

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

            var policy = new TriggeredTcpReconnectionPolicy();

            policy.Trigger();
            var writer = new TcpSocketWriter(IPAddress.Loopback, port, policy, 2);

            var listenerClient = await listener.AcceptTcpClientAsync();

            listenerClient.Close();

            var errors      = new List <Exception>();
            var errorThrown = false;

            writer.LoggingFailureHandler += (ex) =>
            {
                errorThrown = true;
                errors.Add(ex);
            };

            while (!errorThrown)
            {
                writer.Enqueue("boris\r\n");
            }
            for (int i = 0; i < 10; i++)
            {
                writer.Enqueue("boris\r\n");
            }

            policy.Trigger();
            listenerClient = await listener.AcceptTcpClientAsync();

            writer.Dispose();

            var receiverReader = new StreamReader(listenerClient.GetStream());

            // Then check what was left in the queue when we disconnected
            Assert.Equal("boris", await receiverReader.ReadLineAsync());
            Assert.Equal("boris", await receiverReader.ReadLineAsync());
            Assert.Equal(0, listenerClient.Available);
        }
        public async Task TestTcpSocketWriter()
        {
            var listener = new TcpListener(IPAddress.Loopback, 0);
            listener.Start();
            int port = ((IPEndPoint)listener.Server.LocalEndPoint).Port;


            var writer = new TcpSocketWriter(IPAddress.Loopback, port, new ExponentialBackoffTcpReconnectionPolicy(), 10);

            var listenerClient = await listener.AcceptTcpClientAsync();

            writer.Enqueue("This is a test.\r\n");

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

            Assert.Equal(line, "This is a test.");

            listenerClient.Close();
            listener.Stop();
        }
        public async Task TestTcpSocketWriter()
        {
            var listener = new TcpListener(IPAddress.Loopback, 0);

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


            var writer = new TcpSocketWriter(IPAddress.Loopback, port, new ExponentialBackoffTcpReconnectionPolicy(), 10);

            var listenerClient = await listener.AcceptTcpClientAsync();

            writer.Enqueue("This is a test.\r\n");

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

            Assert.Equal(line, "This is a test.");

            listenerClient.Close();
            listener.Stop();
        }
        public async Task TestEventsQueuedWhileWaitingForInitialConnection()
        {
            var listener = new TcpListener(IPAddress.Loopback, 0);

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

            var writer = new TcpSocketWriter(IPAddress.Loopback, port, new ExponentialBackoffTcpReconnectionPolicy(), 100);

            writer.Enqueue("Event 1\r\n");
            writer.Enqueue("Event 2\r\n");

            listener.Start();
            var listenerClient = listener.AcceptTcpClient();
            var receiverReader = new StreamReader(listenerClient.GetStream());

            Assert.Equal("Event 1", await receiverReader.ReadLineAsync());
            Assert.Equal("Event 2", await receiverReader.ReadLineAsync());

            listener.Stop();
            listenerClient.Close();
            writer.Dispose();
        }
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="host">IP address to log to.</param>
 /// <param name="port">Port on remote host.</param>
 /// <param name="policy">An object embodying a reconnection policy for if the 
 /// TCP session drops (defaults to ExponentialBackoffTcpConnectionPolicy).</param>
 /// <param name="maxQueueSize">The maximum number of events to queue if the 
 /// TCP session goes down. If more events are queued, old ones will be dropped
 /// (defaults to 10,000).</param>
 /// <param name="progress">An IProgress instance that will be triggered when
 /// an event is pulled from the queue and written to the TCP port (defaults to a new
 /// Progress object accessible via the Progress property).</param>
 public TcpTraceListener(IPAddress host, int port, ITcpReconnectionPolicy policy, 
                         int maxQueueSize = 10000)
     : base()
 {
     this.writer = new TcpSocketWriter(host, port, policy, maxQueueSize);
 }
Beispiel #10
0
 /// <summary>
 /// Set up a sink.
 /// </summary>
 /// <param name="host">IP address of the host to write to.</param>
 /// <param name="port">TCP port to write to.</param>
 /// <param name="formatter">An object to specify the format events should be
 /// written in (defaults to <code>{timestamp} EventId={...} EventName={...} Level={...} "FormattedMessage={...}"</code>).</param>
 /// <param name="policy">An object defining the reconnect policy in the event
 /// of TCP errors (default: an ExponentialBackoffTcpConnectionPolicy object).</param>
 /// <param name="maxQueueSize">The maximum number of events to queue in the event of
 /// the TCP session dropping before events start to be dropped (defaults to 10,000).</param>
 /// <param name="progress">A progress reporter that triggers when events are written from
 /// the queue to the TCP port (defaults to a new Progress object). It is reachable
 /// via the Progress property.</param>
 public TcpEventSink(IPAddress host, int port, ITcpReconnectionPolicy policy,
                     IEventTextFormatter formatter = null, int maxQueueSize = 10000)
 {
     this.writer    = new TcpSocketWriter(host, port, policy, maxQueueSize);
     this.formatter = formatter;
 }
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="host">IP address to log to.</param>
 /// <param name="port">Port on remote host.</param>
 /// <param name="policy">An object embodying a reconnection policy for if the
 /// TCP session drops (defaults to ExponentialBackoffTcpConnectionPolicy).</param>
 /// <param name="maxQueueSize">The maximum number of events to queue if the
 /// TCP session goes down. If more events are queued, old ones will be dropped
 /// (defaults to 10,000).</param>
 /// <param name="progress">An IProgress instance that will be triggered when
 /// an event is pulled from the queue and written to the TCP port (defaults to a new
 /// Progress object accessible via the Progress property).</param>
 public TcpTraceListener(IPAddress host, int port, ITcpReconnectionPolicy policy,
                         int maxQueueSize = 10000) : base()
 {
     this.writer = new TcpSocketWriter(host, port, policy, maxQueueSize);
 }