基本上,我的目标是让Parallel.ForEach在捕获到特定线程的错误时重复记录日志。
Parallel.ForEach(concurrentLogs, parallelOptions, log =>
try{
//Do work
Console.WriteLine(log);
}
catch(Exception ex) {
concurrentLogs.Enqueue(log); //repeat this log
}
});
因为当我在调试代码时,如果线程捕获到错误(例如IO异常),它就不会再次输出同样的日志信息。
可能的解决方法是什么?
concurrentLogs
的类型。如果它是 BlockingCollection,则需要使用GetConsumingEnumerable()
。 - Scott Chamberlain