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; } }
[Test] public void CreateServer() { SyslogServer srv = new SyslogServer(); srv.Connect(); Assert.IsTrue(srv.Connected, "Server connected"); srv.Close(); }
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; } } }
[Test] public void CreateServerUsing() { using (SyslogServer srv = new SyslogServer()) { srv.Connect(); Assert.IsTrue(srv.Connected, "Server connected"); } }
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(); }
[Test] public void CreateServerTwice() { using (SyslogServer srv1 = new SyslogServer()) { srv1.Connect(); } using (SyslogServer srv2 = new SyslogServer()) { srv2.Connect(); } }
[Test] public void ServerInUse() { using (SyslogServer srv1 = new SyslogServer()) { srv1.Connect(); using (SyslogServer srv2 = new SyslogServer()) { srv2.Connect(); } } }
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>()); } } }
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."); }
private void ToggleSyslogServer(bool start, SyslogServer syslogServer) { if (start) { ChosenOperation['S'] = "StopSyslogServer"; InitStreams(); syslogServer.Start(OnReceivedString, OnException); } else { ChosenOperation['S'] = "StartSyslogServer"; syslogServer.Stop(); DisposeStreams(); } }
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>()); } } }
[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"); } }
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)); }
[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"); } }
public void ServerInUse() { using (SyslogServer srv1 = new SyslogServer()) { srv1.Connect(); using (SyslogServer srv2 = new SyslogServer()) { srv2.Connect(); } } }