private async Task ProcessFiles() { await Task.Run(() => { Stopwatch watchTotal = Stopwatch.StartNew(); try { FileInfo previousFile = null; foreach (var logFile in _logFiles) { if (File.Exists(logFile.FullName)) { if (previousFile != null) { _runningFileBytes += previousFile.Length; } previousFile = logFile; ProgressReportModel fileStartReport = new ProgressReportModel(); fileStartReport.ReportType = ProgressReportTypeEnum.CurrentFile; fileStartReport.CurrentProcessingFile = logFile; _progress.Report(fileStartReport); Stopwatch watch = Stopwatch.StartNew(); ProgressReportModel lineReport = new ProgressReportModel(); using (var reader = new StreamReader(logFile.FullName)) { _currentLine = reader.ReadLine(); int lineRunningNumber = 1; long lineRunningBytes = _currentLine != null ? _currentLine.Length : 0L; while (_currentLine != null) { _cancellationToken.ThrowIfCancellationRequested(); if (lineRunningNumber % 1000 == 0) { lineReport.ReportType = ProgressReportTypeEnum.CurrentLine; lineReport.CurrentProcessingFileRunningLineNumber = lineRunningNumber; lineReport.CurrentProcessingFileRuningBytes = lineRunningBytes; lineReport.CurrentProcessingFileTotalRuningBytes = _runningFileBytes + lineRunningBytes; _progress.Report(lineReport); } Tuple <string, ErrorOperationEnum?> result = ProcessLine(); if (result.Item1 != null) { WriteLine(result.Item1, result.Item2); } _currentLine = reader.ReadLine(); lineRunningNumber++; lineRunningBytes += _currentLine != null ? _currentLine.Length : 0L; } } watch.Stop(); ProgressReportModel fileEndReport = new ProgressReportModel(); fileEndReport.ReportType = ProgressReportTypeEnum.CurrentFileProcessTime; fileEndReport.CurrentFileProcessTime = watch.Elapsed.ToString(); _progress.Report(fileEndReport); } } } finally { ProgressReportModel closingFilesReport = new ProgressReportModel(); closingFilesReport.ReportType = ProgressReportTypeEnum.ClosingFiles; _progress.Report(closingFilesReport); var machineSessionDic = new Dictionary <string, List <string> >(); var currentClosingFileCount = 0; foreach (var sessionPair in _sessionDic) { var sessionId = sessionPair.Key; var session = sessionPair.Value; if (session.Writer != null) { session.Writer.Close(); string newFileName = $@"{_outputPathLogs}\[{session.StartTime}]-[{session.EndTime}][ERROR({session.ErrorCount})][FATAL({session.FatalCount})][{sessionId}][{session.StoreCode}][{session.RegisterNumber}][{session.MachineName}].txt"; if (File.Exists(newFileName)) { File.Delete(newFileName); } File.Move(session.FileName, newFileName); } // Save session ids for each machine if (session.MachineName != null) { string key = $"[{session.MachineName}],[{session.StoreCode}],[{session.RegisterNumber}]"; if (!machineSessionDic.ContainsKey(key)) { machineSessionDic.Add(key, new List <string> { sessionId }); } else { machineSessionDic[key].Add(sessionId); } } currentClosingFileCount++; ProgressReportModel closingCurrentFileReport = new ProgressReportModel(); closingCurrentFileReport.ReportType = ProgressReportTypeEnum.ClosingCurrentFile; closingCurrentFileReport.ClosingFilesProgress = new int[] { currentClosingFileCount, _sessionDic.Count }; _progress.Report(closingCurrentFileReport); } foreach (var errorPair in _errorDic) { var errOperation = errorPair.Key; var error = errorPair.Value; if (error.Writer != null) { error.Writer.Close(); string newFileName = $@"{_outputPathErrors}\[{errOperation}] [{error.StartTime}]-[{error.EndTime}][ERROR({error.ErrorCount})].txt"; if (File.Exists(newFileName)) { File.Delete(newFileName); } File.Move(error.FileName, newFileName); } } if (_errorCountDic.Count > 0) { string fileName = $@"{_outputPathErrors}\ErrorCount.txt"; if (File.Exists(fileName)) { File.Delete(fileName); } using (var writer = new StreamWriter(fileName, true)) { foreach (var pair in _errorCountDic.OrderByDescending(e => e.Value)) { writer.WriteLine($"{pair.Key}: {pair.Value}"); } } } if (_saveTransactionOperations.Count > 0) { string fileName = $@"{_outputPathErrors}\Analysis(SaveTransaction).txt"; if (File.Exists(fileName)) { File.Delete(fileName); } var minCountDic = new Dictionary <string, int>(); var secCountDic = new Dictionary <string, int>(); int totalFailed = 0; using (var writer = new StreamWriter(fileName, true)) { foreach (var operation in _saveTransactionOperations) { if (!operation.Successful) { totalFailed++; } string min = operation.StartTime.Substring(0, 16); string sec = operation.StartTime.Substring(0, 19); if (minCountDic.ContainsKey(min)) { minCountDic[min]++; } else { minCountDic.Add(min, 1); } if (secCountDic.ContainsKey(sec)) { secCountDic[sec]++; } else { secCountDic.Add(sec, 1); } string status = operation.Successful ? "Succ" : "Fail"; if (operation.EndTime == null) { operation.EndTime = "null"; } writer.WriteLine($"{operation.StartTime} - {operation.EndTime.PadLeft(23)} [{operation.Duration.ToString("n3").PadLeft(7)}] [{status}] {operation.SessionId}"); } writer.WriteLine("=============================================="); writer.WriteLine($"Total: {_saveTransactionOperations.Count.ToString("n0")} , Failed: {totalFailed.ToString("n0")}"); writer.WriteLine("==Count per minute============================================"); foreach (var pair in minCountDic.OrderByDescending(e => e.Value)) { writer.WriteLine($"{pair.Key}: {pair.Value}"); } writer.WriteLine("==Count per second============================================"); foreach (var pair in secCountDic.OrderByDescending(e => e.Value)) { writer.WriteLine($"{pair.Key}: {pair.Value}"); } } } if (machineSessionDic.Count > 0) { string fileName = $@"{_outputPathErrors}\MachineSessions.txt"; if (File.Exists(fileName)) { File.Delete(fileName); } using (var writer = new StreamWriter(fileName, true)) { foreach (var pair in machineSessionDic) { foreach (var sid in pair.Value) { writer.WriteLine($"{pair.Key},[{sid}]"); } } } } watchTotal.Stop(); ProgressReportModel totalTimeReport = new ProgressReportModel(); totalTimeReport.ReportType = ProgressReportTypeEnum.TotalProcessTime; totalTimeReport.TotalProcessTime = watchTotal.Elapsed.ToString(); _progress.Report(totalTimeReport); } }); }