/// <summary> /// Send log message to message queue. /// </summary> protected override void Append(LoggingEvent loggingEvent) { string text = RenderLoggingEvent(loggingEvent); string message = text.Replace("(null)", ""); if (!MessageValidation(message)) { return; } bool QueueFailureCountThresholdReached = (QueueFailureCount >= m_QueueFailureCountThreshold); bool QueueRestoreTimeThresholdReached; if (!QueueFailureCountThresholdReached) { QueueRestoreTimeThresholdReached = false; } else { TimeSpan t = System.DateTime.Now - LastQueueFailureTime; QueueRestoreTimeThresholdReached = t.TotalSeconds > m_QueueRestoreTimeThreshold; } if (!QueueFailureCountThresholdReached || QueueRestoreTimeThresholdReached) { if (QueueRestoreTimeThresholdReached) { // Queue Restore Time Threshold is reached. // It's time to clear the failure count and direct message to queue. QueueFailureCount = 0; log.Debug("Queue Restore Time Threshold (" + m_QueueRestoreTimeThreshold.ToString() + ") reached. Message is directed to queue."); } try { ConnectionFactory connectionFactory = new ConnectionFactory(m_QueueURI); NmsTemplate template = new NmsTemplate(connectionFactory); template.ConvertAndSend(m_QueueName, message); QueueFailureCount = 0; } catch (Exception e) { log.Fatal("An error occurred while sending message to queue.", e); QueueHelper.RecordOriginalMessage(message); QueueFailureCount++; LastQueueFailureTime = DateTime.Now; } } else { log.Debug("Queue Failure Count Threshold (" + m_QueueFailureCountThreshold.ToString() + ") reached. Message is directed to local file. "); QueueHelper.RecordOriginalMessage(message); } }
/// <summary> /// Check if message in good XML format. /// </summary> protected bool MessageValidation(string message) { bool result = true; XmlDocument doc = new XmlDocument(); try { doc.LoadXml(message); } catch (Exception e) { log.Fatal("Message format error.", e); QueueHelper.RecordOriginalMessage(message); result = false; } return(result); }