private void ProcessLoop() { try { while (true) { byte[] data = _queue.Dequeue(); try { SyslogMessage msg = SyslogMessage.Parse(data); OnMessageReceived(new SyslogMessageEventArgs(msg)); } catch (FormatException ex) { OnError(new UnhandledExceptionEventArgs(ex, false)); continue; } } } catch (ThreadInterruptedException) { return; } catch (Exception ex) { OnError(new UnhandledExceptionEventArgs(ex, true)); Log.Error("Error receiving Syslog messages from Logbus-ng server"); Log.Debug("Error details: {0}", ex.Message); new Thread(delegate() { try { Stop(); } catch { } }).Start(); return; } }
private void LoopConsumer(object queue) { long reads = 0; try { IFifoQueue <object> q = (IFifoQueue <object>)queue; while (true) { object item = q.Dequeue(); if (item == null) { Interlocked.Increment(ref _errors); } else { reads++; } Thread.Sleep(0); } } catch (ThreadAbortException) { TestContext.WriteLine("Read {0} elements", reads); } }
private void WorkerLoop() { try { while (true) { SyslogMessage message = _messageQueue.Dequeue(); //if (message.Facility == SyslogFacility.Internally) continue; //Redundant SyslogAttributes attrs = message.GetAdvancedAttributes(); DateTime? lastHb = null; string host = message.Host ?? "", process = message.ProcessID ?? message.ApplicationName ?? "", logger = attrs.LogName ?? ""; bool ffda = message.MessageId == "FFDA"; if (message.MessageId == "HEARTBEAT" && message.Severity == SyslogSeverity.Debug) { lastHb = message.Timestamp; } try { string entityName = null; //Suppress error about missing initialization try { DataRow newRow = _entityTable.Rows.Add( host, process, logger, message.ApplicationName ?? "", ffda, message.Timestamp, lastHb, DBNull.Value, DBNull.Value ); entityName = GetEntityName(newRow); Log.Debug("Acquired new entity: {0}, {1}FFDA-enabled", entityName, ffda ? "" : "not "); //Now creating channel for new entity IFilter entityFilter = new EntityFilter(host, process, logger); string description = string.Format("Channel monitoring logs from entity {0}", entityName); do { string randomChannelId = "em_" + Randomizer.RandomAlphanumericString(15); try { _logbus.CreateChannel(randomChannelId, "EntityManager auto-generated", entityFilter, description, 0); //Edit row accordingly newRow[_colChannelId] = randomChannelId; break; } catch (LogbusException) //Duplicate channel ID { continue; } } //Not necessarily a poor choice. With 15 chars we have billions of opportunities. //In a real system, we can't have more than thousands of entities. This algorithm //might go into stall only if randomizer is not "random" enough while (true); if (ffda) //Create FFDA channel too { entityFilter = new EntityFilter(host, process, logger, true); description = string.Format("Channel monitoring FFDA logs from entity {0}", entityName); do { string randomChannelId = "em_" + Randomizer.RandomAlphanumericString(15); try { _logbus.CreateChannel(randomChannelId, "EntityManager auto-generated", entityFilter, description, 0); //Edit row accordingly newRow[_colFfdaChannelId] = randomChannelId; break; } catch (LogbusException) //Duplicate channel ID { continue; } } //Like above while (true); } } catch (ConstraintException) { //We suppose we are trying to insert a duplicate primary key, then now we switch to update DataRow existingRow = _entityTable.Select(GetQuery(message))[0]; bool oldFfda = (bool)existingRow[_colFfda]; existingRow.BeginEdit(); existingRow[_colFfda] = ffda | oldFfda; if (lastHb != null) { existingRow[_colLastHeartbeat] = message.Timestamp; } else { existingRow[_colLastAction] = message.Timestamp; } existingRow.EndEdit(); if (ffda && !oldFfda) { Log.Debug("Entity {0} is now FFDA-enabled", entityName); //Create FFDA channel IFilter entityFilter = new EntityFilter(host, process, logger, true); string description = string.Format("Channel monitoring FFDA logs from entity {0}", entityName); do { string randomChannelId = "em_" + Randomizer.RandomAlphanumericString(15); try { _logbus.CreateChannel(randomChannelId, "EntityManager auto-generated", entityFilter, description, 0); //Edit row accordingly existingRow[_colFfdaChannelId] = randomChannelId; break; } catch (LogbusException) //Duplicate channel ID { continue; } } //Like above while (true); } } } catch (ThreadAbortException) { throw; } catch (Exception ex) { Log.Error("Unable to add an entity row into the data table"); Log.Debug(string.Format("Error details: {0}", ex.Message)); } } } catch (ThreadAbortException) { } }
private void RunnerLoop() { try { while (Running) { SyslogMessage msg = _messageQueue.Dequeue(); Interlocked.Increment(ref _processedMessages); if (_withinCoalescenceWindow || SubscribedClients == 0 || !Filter.IsMatch(msg)) { continue; } try { if (MessageReceived != null) { MessageReceived(this, new SyslogMessageEventArgs(msg)); } } catch (Exception ex) { if (Error != null) { Error(this, new UnhandledExceptionEventArgs(ex, false)); } Log.Error("Unable to forward messages from channel {0} via event", Name); Log.Debug("Error details: {0}", ex.Message); } _transportLock.AcquireReaderLock(DEFAULT_JOIN_TIMEOUT); try { foreach (KeyValuePair <string, IOutboundTransport> kvp in _transports) { try { kvp.Value.SubmitMessage(msg); } catch (ObjectDisposedException) { LockCookie ck = _transportLock.UpgradeToWriterLock(DEFAULT_JOIN_TIMEOUT); try { _transports.Remove(kvp.Key); } finally { _transportLock.DowngradeFromWriterLock(ref ck); } } } catch (Exception ex) { if (Error != null) { Error(this, new UnhandledExceptionEventArgs(ex, false)); } Log.Error("Unable to forward messages from channel {0} to transport {1}", Name, kvp.Key); Log.Debug("Error details: {0}", ex.Message); } } finally { _transportLock.ReleaseReaderLock(); } Interlocked.Increment(ref _deliveredMessages); if (CoalescenceWindowMillis > 0) { _coalescenceTimer = new Timer(ResetCoalescence, null, (int)CoalescenceWindowMillis, Timeout.Infinite); _withinCoalescenceWindow = true; } }
private void DeliveryLoop() { try { while (true) { try { if (_client == null) { if (_host == null) { throw new InvalidOperationException("Remote address not specified"); } if (_port < 1 || _port > 65535) { _port = SyslogTlsReceiver.TLS_PORT; } _log.Debug("Creating new TcpClient for SyslogTlsCollector to {0}:{1}", _host, _port); _client = new TcpClient { NoDelay = true, SendBufferSize = 1048576, SendTimeout = 3600000 }; } else if (!_client.Connected) { try { _log.Debug("Connecting TlsCollector to host {0}:{1}", _host, _port); // ReSharper disable AssignNullToNotNullAttribute _client.Connect(_host, _port); // ReSharper restore AssignNullToNotNullAttribute _remoteStream = new SslStream(_client.GetStream(), false, TlsServerValidator, TlsClientSelector) { WriteTimeout = 3600000 }; //_remoteStream.AuthenticateAsClient(_host, null, SslProtocols.Tls, true); // ReSharper disable AssignNullToNotNullAttribute _remoteStream.AuthenticateAsClient(_host); // ReSharper restore AssignNullToNotNullAttribute _log.Debug("TlsCollector connected to host {0}:{1}", _host, _port); } catch (ObjectDisposedException) { _client = null; _remoteStream = null; continue; } } catch (Exception ex) { _log.Error("Error connecting TlsCollector to host {0}:{1}", _host, _port); _log.Debug("Error details: {0}", ex.Message); throw new LogbusException("Unable to log to remote TLS host", ex); } SyslogMessage[] msgs = _queue.Flush(); if (msgs == null || msgs.Length == 0) { msgs = new[] { _queue.Dequeue() } } ; byte[] data; using (MemoryStream ms = new MemoryStream(8192)) { foreach (SyslogMessage msg in msgs) { byte[] payload = Encoding.UTF8.GetBytes(msg.ToRfc5424String()); foreach (char c in payload.Length.ToString(CultureInfo.InvariantCulture)) { ms.WriteByte((byte)c); } ms.WriteByte((byte)' '); ms.Write(payload, 0, payload.Length); } data = ms.ToArray(); } // ReSharper disable PossibleNullReferenceException _remoteStream.Write(data, 0, data.Length); _remoteStream.Flush(); // ReSharper restore PossibleNullReferenceException } catch (ObjectDisposedException) { _client = null; _remoteStream = null; _log.Error("TLS server {0}:{1} closed connection. Re-opening it", _host, _port); continue; } }