コード例 #1
0
        /// <summary>
        /// Process the log
        /// use the patter to process async
        /// https://docs.microsoft.com/en-us/dotnet/standard/collections/thread-safe/blockingcollection-overview
        /// </summary>
        /// <returns></returns>
        public async Task PerformOldAsync()
        {
            // // init the publishers
            // await CreatePublishersAsync();

            // query
            BlockingCollection <DecoratedAuditLogEntry> dataItems = new BlockingCollection <DecoratedAuditLogEntry>(100);
            AuditLogQueryResult result;

            do
            {
                result = await AuditLog.QueryLogAsync(0, null);

                // Blocks if numbers.Count == dataItems.BoundedCapacity
                foreach (DecoratedAuditLogEntry entry in result.DecoratedAuditLogEntries)
                {
                    //TODO: is there a better way to add a batch?
                    // i.e. AddRange()
                    dataItems.Add(entry);
                }

                // Let consumer know we are done.
                dataItems.CompleteAdding();
            } while (result.HasMore);

            // A simple blocking consumer with no cancellation.
            int entryCount = 0;

            while (!dataItems.IsCompleted)
            {
                DecoratedAuditLogEntry entry = null;
                // Blocks if number.Count == 0
                // IOE means that Take() was called on a completed collection.
                // Some other thread can call CompleteAdding after we pass the
                // IsCompleted check but before we call Take.
                // In this example, we can simply catch the exception since the
                // loop will break on the next iteration.
                try
                {
                    entry = dataItems.Take();
                }
                catch (InvalidOperationException) { }

                if (entry != null)
                {
                    // AddEntriesToPublish(entry);
                }

                if (++entryCount % 100 == 0)
                {
                    // publish once every 100 entries
                    PublishEntries();
                }
            }

            PublishEntries();
            Console.WriteLine("\r\nNo more items to take.");
        }
コード例 #2
0
        private byte[] GetEventData(DecoratedAuditLogEntry entry)
        {
            StringBuilder payload = new StringBuilder();

            using (StringWriter stringWriter = new StringWriter(payload))
            {
                JsonSerializer.CreateDefault().Serialize(stringWriter, entry);
            }
            return(Encoding.UTF8.GetBytes(payload.ToString()));
        }
コード例 #3
0
 public void Add(DecoratedAuditLogEntry entry)
 {
     EventsToPublish.Add(entry);
 }