Пример #1
0
        /// <summary>
        ///     This action runs in the background and uploads the items in the local queue
        ///     to an azure queue
        /// </summary>
        private void UploadItems()
        {
            var batch = new AnalyticsItemList();

            while (true)
            {
                try
                {
                    AnalyticsItem item;
                    if (this.localQueue.TryDequeue(out item))
                    {
                        batch.Add(item);
                        if (batch.Count >= BatchSize)
                        {
                            SaveBatch(batch);
                        }
                    }
                    else
                    {
                        SaveBatch(batch);

                        // not a lot of items in the queue. sleep for one second
                        Thread.Sleep(1000);
                    }

                    // update counter to indicate currentq ueue length
                    Counter.SetValue(CounterNames.AnalyticsLocalQueueLength, this.localQueue.Count);
                }
                catch (ThreadAbortException)
                {
                    break;
                }
                catch (Exception ex)
                {
                    // never exit
                    Log.Error(ex, "Error saving analytics batch");
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Parse a list of items form an Xml String
        /// </summary>
        /// <param name="str">
        /// the input string
        /// </param>
        /// <returns>
        /// a list of items
        /// </returns>
        public static AnalyticsItemList Deserialize(string str)
        {
            try
            {
                var serializerSettings = new JsonSerializerSettings
                {
                    NullValueHandling = NullValueHandling.Ignore
                };
                return(JsonConvert.DeserializeObject <AnalyticsItemList>(str, serializerSettings));
            }
            catch (Exception e)
            {
                Log.Verbose("Original String:{0}; Error: {1}", str, e);
                //Don't do anything this is expected until all the systems will migrate to json
            }
            // Backward compatability to xml
            var list = new AnalyticsItemList();

            try
            {
                XElement document = XElement.Parse(str);
                foreach (XElement node in document.Elements())
                {
                    AnalyticsItem item = AnalyticsItem.Parse(node);
                    if (item != null)
                    {
                        list.Add(item);
                    }
                }
            }
            catch (Exception e)
            {
                // log and ignore the invalid message
                Log.Error(e, "Original String:{0}", str);
            }

            return(list);
        }