我有一個奇怪的情況下,我無法找到我的錯誤的根源以下堆棧跟蹤:布爾包括任務取消的異常而在C#做Parallel.Foreach
Message :One or more errors occurred.<br/>
StackTrace : at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at System.Threading.Tasks.Task.Wait()
at System.Threading.Tasks.Parallel.ForWorker[TLocal](Int32 fromInclusive, Int32 toExclusive, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Func`4 bodyWithLocal, Func`1 localInit, Action`1 localFinally)
at System.Threading.Tasks.Parallel.For(Int32 fromInclusive, Int32 toExclusive, ParallelOptions parallelOptions, Action`1 body)
代碼本身是有一些邏輯簡單明瞭Parallel.Foreach循環像下面:
Parllel.Foreach(_collection, new ParallelOptions { MaxDegreeOfParallelism =5 }, item=>
{
// code inside
});
可能是什麼這個問題的根源?你們有什麼提示給我,我應該在哪裏尋找?
@mortb這是我的日誌堆棧跟蹤異常:
string filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\Error.txt";
using (StreamWriter writer = new StreamWriter(filePath, true))
{
writer.WriteLine("Message :" + ex.Message + "<br/>" + Environment.NewLine + "StackTrace :" + ex.StackTrace +
"" + Environment.NewLine + "Date :" + DateTime.Now.ToString());
writer.WriteLine(Environment.NewLine + "-----------------------------------------------------------------------------" + Environment.NewLine);
}
調試,並採取一看拋出的異常屬性'InnerExceptions'(注意'InnerExceptions'的S)外的例外是剛剛超過一個的包裝或幾個拋出異常通過''// code inside''「,實際的異常被列在'InnerExceptions'中 – mortb
@mortb這就是我的錯誤..我無法將異常寫入文本文件...等待我將顯示你我如何記錄錯誤.. – User987
@mortb你能看到我更新的問題嗎?這就是我如何登錄異常本身...有沒有什麼辦法可以記錄內部異常本身並查看哪條線會拋出它? – User987