コード例 #1
0
        /// <summary>
        /// Publishes a list of events to all registered network endpoints after
        /// applying the required event format transforms. Stored events will be
        /// deleted if publishing was successful for all registered endpoints.
        /// This method should NOT be called from the main thread.
        /// </summary>
        /// <param name="events"></param>
        /// <returns></returns>
        public async Task PublishEventsAsync(List <InstrumentationEvent> events)
        {
            if (events == null || events.Count == 0)
            {
                return;
            }
            var eventIds     = new List <string>();
            var success      = true;
            var remoteKeySet = remotes.Keys;

            foreach (var key in remoteKeySet)
            {
                ITransform transformer = key;
                var        eventsArray = new JArray();
                if (transformer != null)
                {
                    foreach (var instrumentationEvent in events)
                    {
                        eventIds.Add(instrumentationEvent.EventId);
                        var eventJson = transformer.Transform(instrumentationEvent);
                        if (eventJson != null)
                        {
                            eventsArray.Add(eventJson);
                        }
                    }
                }
                var networkPublisher = remotes[key];
                if (networkPublisher != null)
                {
                    var networkSuccess = await networkPublisher.PublishAsync(eventsArray);

                    /*
                     * Updates the success flag only if all previous requests have been
                     * successful. This ensures that the operation is marked success only
                     * if all publishers are successful.
                     */
                    if (success)
                    {
                        success = networkSuccess;
                    }
                }
            }

            if (success)
            {
                await eventStoreManager.DeleteEventsAsync(eventIds);
            }
        }