Пример #1
0
        private void Connect()
        {
            if (_server != null)
            {
                return;
            }

            try
            {
                IPAddress ip = _specificIP;
                if (ip == null)
                {
                    ip = SyslogServer.DefaultAddress;
                }
                _server = new SyslogServer(ip, _port);
                _server.SyslogMessageReceived += new
                                                 SyslogServer.SyslogMessageDelegate(OnSyslogMessage);
                _server.Connect();
            }
            catch (Exception)
            {
                if (_server != null)
                {
                    _server.Close();
                    _server = null;
                }
                throw;
            }
        }
Пример #2
0
		[Test] public void CreateServer()
		{
			SyslogServer srv = new SyslogServer();
			srv.Connect();
			Assert.IsTrue(srv.Connected, "Server connected");
			srv.Close();
		}
Пример #3
0
        public void StartLogging()
        {
            lock (this)
            {
                if (_server != null)
                {
                    return;
                }

                try
                {
                    // New server
                    _server = new SyslogServer(SyslogServer.DefaultAddress, _port);

                    // Register for events
                    _server.SyslogMessageReceived += _eventDelegate;

                    // Start listening
                    _server.Connect();
                }
                catch (Exception)
                {
                    if (_server != null)
                    {
                        _server.Close();
                        _server.SyslogMessageReceived -= _eventDelegate;
                        _server = null;
                    }

                    throw;
                }
            }
        }
Пример #4
0
 [Test] public void CreateServerUsing()
 {
     using (SyslogServer srv = new SyslogServer())
     {
         srv.Connect();
         Assert.IsTrue(srv.Connected, "Server connected");
     }
 }
Пример #5
0
        [Test] public void CreateServer()
        {
            SyslogServer srv = new SyslogServer();

            srv.Connect();
            Assert.IsTrue(srv.Connected, "Server connected");
            srv.Close();
        }
Пример #6
0
		[Test] public void CreateServerUsing()
		{
			using (SyslogServer srv = new SyslogServer())
			{
				srv.Connect();
				Assert.IsTrue(srv.Connected, "Server connected");
			}
		}
Пример #7
0
 private static void StartSyslogServer()
 {
     _server = new SyslogServer(514);
     _server.MessageReceived += (sender, eventArgs) =>
     {
         var message = eventArgs.Message.Text;
         Console.WriteLine(message);
         Task.Factory.StartNew(async(s) => await WriteMessageToLogFile(s.ToString()), message);
     };
     _server.Start();
 }
Пример #8
0
		[Test] public void CreateServerTwice()
		{
			using (SyslogServer srv1 = new SyslogServer())
			{
				srv1.Connect();
			}

			using (SyslogServer srv2 = new SyslogServer())
			{
				srv2.Connect();
			}
		}
Пример #9
0
        [Test] public void ServerInUse()
        {
            using (SyslogServer srv1 = new SyslogServer())
            {
                srv1.Connect();

                using (SyslogServer srv2 = new SyslogServer())
                {
                    srv2.Connect();
                }
            }
        }
Пример #10
0
        [Test] public void CreateServerTwice()
        {
            using (SyslogServer srv1 = new SyslogServer())
            {
                srv1.Connect();
            }

            using (SyslogServer srv2 = new SyslogServer())
            {
                srv2.Connect();
            }
        }
Пример #11
0
        public void ServerInUse()
        {
            using (SyslogServer srv1 = new SyslogServer())
            {
                srv1.Connect();
                // Assert.That(() => srv1.Connect(), Throws.TypeOf<SocketException>());

                using (SyslogServer srv2 = new SyslogServer())
                {
                    // srv2.Connect();
                    Assert.That(() => srv2.Connect(), Throws.TypeOf <SocketException>());
                }
            }
        }
Пример #12
0
        static void Main(string[] args)
        {
            var syslogServer = new SyslogServer(listenAddress: IPAddress.Any);

            syslogServer.Start(OnMessageReceived);

            Console.WriteLine($"Server ready. Send syslog messages to {syslogServer.ListenAddress.ToString()} UDP port {syslogServer.UdpListenPort} or TCP port {syslogServer.TcpListenPort}.");
            Console.WriteLine("Press [ENTER] to stop");

            Console.ReadLine();
            syslogServer.Stop();

            Console.WriteLine("Server stopped.");
        }
Пример #13
0
 private void ToggleSyslogServer(bool start, SyslogServer syslogServer)
 {
     if (start)
     {
         ChosenOperation['S'] = "StopSyslogServer";
         InitStreams();
         syslogServer.Start(OnReceivedString, OnException);
     }
     else
     {
         ChosenOperation['S'] = "StartSyslogServer";
         syslogServer.Stop();
         DisposeStreams();
     }
 }
Пример #14
0
        public void ServerInUse()
		{
			using (SyslogServer srv1 = new SyslogServer())
			{
				srv1.Connect();
                // Assert.That(() => srv1.Connect(), Throws.TypeOf<SocketException>());
	
				using (SyslogServer srv2 = new SyslogServer())
				{
					// srv2.Connect();
                    Assert.That(() => srv2.Connect(), Throws.TypeOf<SocketException>());
				}
			}


            

		}
Пример #15
0
        [Test] public void TransferTest()
        {
            using (SyslogServer srv = new SyslogServer())
            {
                srv.SyslogMessageReceived += new SyslogServer.SyslogMessageDelegate
                                                 (OnSyslogMessageReceived);

                srv.Connect();

                _messageCount = 0;
                SyslogMessage msg = new SyslogMessage("localhost", MessageText);

                using (SyslogUdpClient cl = new SyslogUdpClient())
                {
                    cl.Connect(IPAddress.Loopback);

                    for (int i = 0; i < MessagesToSend; i++)
                    {
                        cl.Send(msg);
                        // Allow a small delay so not to overload
                        Thread.Sleep(10);
                    }
                }

                // Sleep until message counts are settled
                int prevCount = -1;
                int newCount  = 0;
                while (prevCount != newCount)
                {
                    Thread.Sleep(100);
                    lock (this)
                    {
                        prevCount = newCount;
                        newCount  = _messageCount;
                    }
                }

                Assert.AreEqual(MessagesToSend, _messageCount, "Messages received");
            }
        }
Пример #16
0
        private void ToggleSyslogServer(bool start, SyslogServer syslogServer)
        {
            buttonStartStopSyslogServer.Enabled = false;

            var operation = start ? "Stop" : "Start";

            buttonStartStopSyslogServer.Name = $"button{operation}SyslogServer";
            buttonStartStopSyslogServer.Text = $@"{operation} Syslog Server";

            if (start)
            {
                syslogServer.Start(receivedStringAction, exceptionAction);
            }
            else
            {
                syslogServer.Stop();
            }

            Action enableButton = () => buttonStartStopSyslogServer.Enabled = true;

            Task.Delay(500).ContinueWith(_ => Invoke(enableButton));
        }
Пример #17
0
		[Test] public void TransferTest()
		{
			using (SyslogServer srv = new SyslogServer())
			{
				srv.SyslogMessageReceived += new SyslogServer.SyslogMessageDelegate
					(OnSyslogMessageReceived);

				srv.Connect();

				_messageCount = 0;
				SyslogMessage msg = new SyslogMessage("localhost", MessageText);

				using (SyslogUdpClient cl = new SyslogUdpClient())
				{
                    cl.Connect(IPAddress.Loopback);

					for (int i=0; i<MessagesToSend; i++)
					{
						cl.Send(msg);
						// Allow a small delay so not to overload
						Thread.Sleep(10);
					}
				}

				// Sleep until message counts are settled
				int prevCount = -1;
				int newCount = 0;
				while (prevCount != newCount)
				{
					Thread.Sleep(100);
					lock (this)
					{
						prevCount = newCount;
						newCount = _messageCount;
					}
				}

				Assert.AreEqual(MessagesToSend, _messageCount, "Messages received");
			}
		}
Пример #18
0
        public void ServerInUse()
        {
            using (SyslogServer srv1 = new SyslogServer())
            {
                srv1.Connect();

                using (SyslogServer srv2 = new SyslogServer())
                {
                    srv2.Connect();
                }
            }
        }