public void ProduceLogs(int count, int buffSize) { LogGenerator g = new LogGenerator(); BlockingCollection<LogEntry> queue = new BlockingCollection<LogEntry>(buffSize); Completed = Task.Factory.StartNew(() => Write(queue, count)); for (int i = 0; i < count; i++) { g.Next(); var entry = new LogEntry() { format = g.FormatStr, parameters = new object[] { g.Param1, g.Param2, g.Param3, g.Param4, g.Param5, g.Param6 } }; queue.Add(entry); } }
public void ProduceLogs(int count, int buffSize) { var bufferOptions = new DataflowBlockOptions() { BoundedCapacity = buffSize, MaxMessagesPerTask = 10 }; var writerOptions = new ExecutionDataflowBlockOptions() { BoundedCapacity = 10, MaxDegreeOfParallelism = 1, MaxMessagesPerTask = 10 }; var serializerOptions = new ExecutionDataflowBlockOptions() { BoundedCapacity = 80, MaxDegreeOfParallelism = 8, SingleProducerConstrained = true, MaxMessagesPerTask = 10 }; LogGenerator g = new LogGenerator(); var file = new StreamWriter("basic.async.srlz.buff.log", false); BufferBlock<LogEntry> buffer = new BufferBlock<LogEntry>(bufferOptions); TransformBlock<LogEntry, string> serializer = new TransformBlock<LogEntry, string>( e => string.Format(e.format, e.parameters), serializerOptions); ActionBlock<string> writer = new ActionBlock<string>(s => file.WriteLine(s), writerOptions); buffer.LinkTo(serializer, new DataflowLinkOptions() { PropagateCompletion = true }); serializer.LinkTo(writer, new DataflowLinkOptions() { PropagateCompletion = true }); for (int i = 0; i < count; i++) { g.Next(); var entry = new LogEntry() { format = g.FormatStr, parameters = new object[] { g.Param1, g.Param2, g.Param3, g.Param4, g.Param5, g.Param6 } }; buffer.SendAsync(entry).Wait(); } buffer.Complete(); Completed = writer.Completion.ContinueWith(t => file.Close()); }