public void AddStageEntry([NotNull] StageEntry se, [NotNull] RunningConfig config) { if (!config.MakeStageEntries) { return; } Console.WriteLine("Config in stage: " + JsonConvert.SerializeObject(config, Formatting.Indented)); if (!string.IsNullOrWhiteSpace(se.DevelopmentStatus)) { se.ImplementationFinished = false; } // ReSharper disable once InconsistentlySynchronizedField string stagePath = Path.Combine(_config.Directories.BaseProcessingDirectory, "stages.xlsx"); string lockPath = stagePath + ".lock"; Random rnd = new Random(); try { using (ILock fileLock = new FileLock(lockPath)) { try { bool lockAquired = false; while (!lockAquired) { try { fileLock.TryAcquire(TimeSpan.FromSeconds(5)); lockAquired = true; } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) { #pragma warning restore CA1031 // Do not catch general exception types Console.WriteLine("Locking failed for file: " + stagePath + ": " + ex.Message); Thread.Sleep(rnd.Next(500)); } } WriteXlsFileContent(stagePath, se); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex1) { #pragma warning restore CA1031 // Do not catch general exception types Console.WriteLine("trying to log result file, exception : " + ex1.Message); } finally { try { fileLock.Dispose(); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception x) { #pragma warning restore CA1031 // Do not catch general exception types Console.WriteLine("failed to release lock, exception : " + x.Message); } } } } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) { #pragma warning restore CA1031 // Do not catch general exception types #pragma warning restore CA1031 // Do not catch general exception types Console.WriteLine("Locking release failed for file: " + stagePath + ": " + ex.Message); Thread.Sleep(rnd.Next(500)); } }
private static void WriteXlsFileContent([NotNull] string stagePath, [NotNull] StageEntry se) { var existingFile = new FileInfo(stagePath); using (var package = new ExcelPackage(existingFile)) { //get the first worksheet in the workbook if (package.Workbook.Worksheets.Count == 0) { package.Workbook.Worksheets.Add("Progress"); } if (package.Workbook?.Worksheets[1]?.Cells[1, 1]?.Value?.ToString() != "Key") { var ws = package.Workbook.Worksheets[1]; ws.View.FreezePanes(2, 1); int headerCol = 1; ws.Column(headerCol).Width = 15; ws.Cells[1, headerCol++].Value = "Key"; ws.Cells[1, headerCol++].Value = "Last Execution"; ws.Cells[1, headerCol++].Value = "StageNumber"; ws.Cells[1, headerCol++].Value = "Name"; ws.Cells[1, headerCol++].Value = "Duration [s]"; ws.Cells[1, headerCol++].Value = "Files Created"; ws.Cells[1, headerCol++].Value = "MakeChartFunction executed"; ws.Cells[1, headerCol++].Value = "Implementation Finished"; ws.Cells[1, headerCol++].Value = "MakeChartFunctionExecuted"; ws.Cells[1, headerCol++].Value = "Is a slice visualizer used"; ws.Cells[1, headerCol++].Value = "Name of Slice Visualizer"; // ReSharper disable once RedundantAssignment ws.Cells[1, headerCol++].Value = "Todos"; } var worksheet = package.Workbook.Worksheets[1]; var rowCount = worksheet.Dimension.End.Row; var targetRow = 0; for (var row = 1; row <= rowCount; row++) { if (worksheet.Cells[row, 1].Value.ToString() == se.Key) { targetRow = row; break; } } if (targetRow == 0) { targetRow = rowCount + 1; worksheet.Cells[targetRow, 1].Value = se.Key; } int col = 2; worksheet.Cells[targetRow, col++].Value = se.Stage.ToString(); worksheet.Cells[targetRow, col++].Value = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString(); worksheet.Cells[targetRow, col++].Value = se.StageNumber; worksheet.Cells[targetRow, col++].Value = se.Name; worksheet.Cells[targetRow, col++].Value = se.Seconds; worksheet.Cells[targetRow, col++].Value = se.FilesCreated; worksheet.Cells[targetRow, col++].Value = se.ImplementationFinished; worksheet.Cells[targetRow, col++].Value = se.MakeChartFunctionExecuted; worksheet.Cells[targetRow, col++].Value = se.IsSliceVisualizerSet; worksheet.Cells[targetRow, col++].Value = se.SliceVisualizerName; // ReSharper disable once RedundantAssignment worksheet.Cells[targetRow, col++].Value = se.DevelopmentStatus; worksheet.Row(targetRow).Style.Fill.PatternType = ExcelFillStyle.Solid; if (se.ImplementationFinished) { worksheet.Row(targetRow).Style.Fill.BackgroundColor.SetColor(0, 64, 250, 200); } else { worksheet.Row(targetRow).Style.Fill.BackgroundColor.SetColor(0, 250, 200, 64); } package.Save(); } }