Exemple #1
0
        private void StartServiceBusQueueListener()
        {
            ThreadPool.QueueUserWorkItem((state) =>
            {
                OnPremisesBufferedTraceListener parent = state as OnPremisesBufferedTraceListener;
                bool canRun = parent.IsRunning;

                try
                {
                    while (canRun)
                    {
                        try
                        {
                            var traceEventBatch = parent.QueueStorage.Get <TraceEventRecord[]>(parent.QueueName, parent.QueueEventBatchSize);

                            if (traceEventBatch != null && traceEventBatch.Count() > 0)
                            {
                                var traceEvents = (from batch in traceEventBatch select batch.AsEnumerable()).Aggregate((i, j) => i.Concat(j));

                                parent.ServiceBusRetryPolicy.ExecuteAction(() =>
                                {
                                    parent.DiagnosticServiceClient.TraceEvent(traceEvents.ToArray <TraceEventRecord>());
                                });

                                foreach (var item in traceEventBatch)
                                {
                                    parent.QueueStorage.Delete <TraceEventRecord[]>(item);
                                }
                            }
                            else
                            {
                                // Time to check if we are still allowed to run. We should run until the in-memory queue is completely flushed or
                                // as long as the parent is running.
                                canRun = !parent.InMemoryQueue.IsEmpty || parent.IsRunning;

                                // No messages were dequeued, let's sleep for the defined interval.
                                if (canRun && parent.QueueReaderSleepTimeout > 0)
                                {
                                    Thread.Sleep(parent.QueueReaderSleepTimeout);
                                }
                            }
                        }
                        catch (ThreadAbortException threadEx)
                        {
                            HandleException(threadEx);
                            return;
                        }
                        catch (Exception ex)
                        {
                            HandleException(ex);

                            // Enforce a sleep interval whenever an exception is encountered.
                            if (parent.QueueReaderSleepTimeout > 0)
                            {
                                Thread.Sleep(parent.QueueReaderSleepTimeout);
                            }
                        }
                    }
                }
                finally
                {
                    parent.QueueReaderDone.Set();
                }
            }, this);
        }
Exemple #2
0
        private void StartInMemoryQueueListener()
        {
            ThreadPool.QueueUserWorkItem((state) =>
            {
                OnPremisesBufferedTraceListener parent = state as OnPremisesBufferedTraceListener;
                IList <TraceEventRecord> traceEvents   = new List <TraceEventRecord>(parent.QueueEventBatchSize);
                TraceEventRecord traceEvent;
                bool canRun = parent.IsRunning;

                try
                {
                    while (canRun)
                    {
                        try
                        {
                            while (!parent.InMemoryQueue.IsEmpty && traceEvents.Count < parent.QueueEventBatchSize)
                            {
                                if (parent.InMemoryQueue.TryDequeue(out traceEvent))
                                {
                                    traceEvents.Add(traceEvent);
                                }
                            }

                            if (traceEvents.Count > 0)
                            {
                                parent.QueueStorage.Put <TraceEventRecord[]>(parent.QueueName, traceEvents.ToArray <TraceEventRecord>());
                                traceEvents.Clear();
                            }
                            else
                            {
                                // Time to check if we are still allowed to run. We should run until the in-memory queue is completely flushed or
                                // as long as the parent is running.
                                canRun = !parent.InMemoryQueue.IsEmpty || parent.IsRunning;

                                // No messages were dequeued, let's sleep for the defined interval.
                                if (canRun && parent.QueueWriterSleepTimeout > 0)
                                {
                                    Thread.Sleep(parent.QueueWriterSleepTimeout);
                                }
                            }
                        }
                        catch (ThreadAbortException threadEx)
                        {
                            HandleException(threadEx);
                            return;
                        }
                        catch (Exception ex)
                        {
                            HandleException(ex);

                            // Enforce a sleep interval whenever an exception is encountered.
                            if (parent.QueueWriterSleepTimeout > 0)
                            {
                                Thread.Sleep(parent.QueueWriterSleepTimeout);
                            }
                        }
                    }
                }
                finally
                {
                    parent.QueueWriterDone.Set();
                }
            }, this);
        }