Ejemplo n.º 1
0
        /// <summary>
        /// Creates the batch.
        /// </summary>
        /// <param name="hubClient">The hub client.</param>
        /// <param name="options">The options.</param>
        /// <returns>ActionBlock&lt;IEnumerable&lt;System.Byte[]&gt;&gt;.</returns>
        public static ActionBlock<IEnumerable<byte[]>> CreateBatch(EventHubClient hubClient, ExecutionDataflowBlockOptions options)
        {
            return new ActionBlock<IEnumerable<byte[]>>(async batch =>
            {
                var size = 0;

                var output = batch.Select(data => {
                    //var index = Array.IndexOf(data, (byte)'>');
                    //var key = index < 0 ? null : Encoding.UTF8.GetString(data, 0, index);
                    size += data.Length;
                    return new EventData(data);// { PartitionKey = key };
                }).ToList();

                Logger.Debug("Sending {Count} events ({Size} bytes)", output.Count, size);
                using (Logger.BeginTimedOperation("SendBatch (" + output.Count + ")", null, LogEventLevel.Debug))
                    await Task.WhenAll(output.GroupBy(x => x.PartitionKey).Select(x => hubClient.SendPartitionedBatchAsync(x, true)));
            }, options);
        }