private static void CreateForRectangle(Rectangle rect) { if (KeepTime <= TimeSpan.Zero || MaxTotalSize <= 0) { return; // Nothing to do } try { string fileName = GetFileNameWithoutExtension(); if (fileName != null) { // Source: http://stackoverflow.com/a/5049138/143684 // Source: http://stackoverflow.com/a/1163770/143684 using (Bitmap bitmap = new Bitmap(rect.Width, rect.Height)) { using (Graphics graphics = Graphics.FromImage(bitmap)) { graphics.CopyFromScreen(rect.X, rect.Y, 0, 0, bitmap.Size, CopyPixelOperation.SourceCopy); } // Save bitmap as PNG and JPEG bitmap.Save(fileName + ".png", ImageFormat.Png); SaveJpegImage(bitmap, fileName + ".jpg", 75); // Evaluate both file sizes and decide which to keep long pngSize = new FileInfo(fileName + ".png").Length; long jpgSize = new FileInfo(fileName + ".jpg").Length; if (pngSize > jpgSize * 2) { File.Delete(fileName + ".png"); } else { File.Delete(fileName + ".jpg"); } } Purge(); } } catch (Exception ex) { FL.Error(ex, "Creating screenshot"); } }
private void StartSubmitTool() { if (sendCheckBox.Checked) { string exeFile = Application.ExecutablePath; if (!string.IsNullOrEmpty(exeFile)) { exeFile = Path.GetDirectoryName(exeFile); exeFile = Path.Combine(exeFile, "LogSubmit.exe"); if (File.Exists(exeFile)) { // Found the log submit tool, now start it try { Process.Start(exeFile, "/errordlg /logpath \"" + FL.LogFileBasePath + "\""); } catch (Exception ex) { // Start failed, show an error message FL.Critical(ex, "Starting log submit tool"); MessageBox.Show( "The log submit tool could not be started." + " " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } return; } } // Log submit tool not found but logs should be sent, show an error message FL.Error("Could not start log submit tool, path or file not found"); MessageBox.Show( "The log submit tool could not be started. The path or file was not found. Please start the tool manually from the application installation directory.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// Advances the enumerator to the next log item of the currently read log file. If there /// are no more items in this file and there is a NextReader set, the first log item of the /// next file reader is selected. If there are no more items in this file and WaitMode is /// set, the method will block until another log item is appended to the current file or /// the wait operation is cancelled by a close event. /// </summary> /// <returns>true if the enumerator was successfully advanced to the next log item; /// false if the enumerator has passed the end of the collection.</returns> public bool MoveNext() { FieldLogFileReader nextReader = null; do { if (nextReader != null) { FL.Trace(reader.ItemCount + " items read from " + Path.GetFileName(reader.FileName)); reader = nextReader; FL.Trace("Switching to next reader " + Path.GetFileName(reader.FileName)); } try { item = reader.ReadLogItem(); } catch (Exception ex) { FL.Error(ex, "Reading item from log file"); OnError(ex); // Skip the rest of the current file and continue with the next one if // available. If this is the last file and WaitMode is set, this priority will // not be monitored anymore. item = null; } if (item == null && reader.IsClosing) { // Close event must have been set Dispose(); return(false); } nextReader = reader.NextReader; }while (item == null && nextReader != null); return(item != null); }
/// <summary> /// Purges screenshot files. /// </summary> public static void Purge() { try { string basePath = FL.LogFileBasePath; if (basePath == null) { return; // Nothing to do } string logDir = Path.GetDirectoryName(basePath); string logFile = Path.GetFileName(basePath); DateTime purgeTime = FL.UtcNow.Subtract(KeepTime); foreach (string fileName in Directory.GetFiles(logDir, logFile + "-scr-*.*")) { FileInfo fi = new FileInfo(fileName); if (fi.LastWriteTimeUtc < purgeTime) { // File is old enough to be deleted try { File.Delete(fileName); } catch { // Retry next time (might be locked by a log viewer reading the file) } } } // Keep maximum data size string[] fileNames = Directory.GetFiles(logDir, logFile + "-scr-*.*"); DateTime[] fileTimes = new DateTime[fileNames.Length]; long[] fileSizes = new long[fileNames.Length]; long totalUsedSize = 0; for (int i = 0; i < fileNames.Length; i++) { FileInfo fi = new FileInfo(fileNames[i]); fileTimes[i] = fi.LastWriteTimeUtc; fileSizes[i] = fi.Length; totalUsedSize += fileSizes[i]; } while (totalUsedSize > MaxTotalSize) { // Find oldest file int oldestIndex = -1; DateTime oldestTime = DateTime.MaxValue; for (int i = 0; i < fileTimes.Length; i++) { if (fileTimes[i] < oldestTime) { oldestTime = fileTimes[i]; oldestIndex = i; } } if (oldestIndex == -1) { break; // Nothing more to delete } // Delete the file and reduce the total size try { File.Delete(fileNames[oldestIndex]); totalUsedSize -= fileSizes[oldestIndex]; } catch { // Try the next file } fileTimes[oldestIndex] = DateTime.MaxValue; // Don't consider this file again } } catch (Exception ex) { FL.Error(ex, "Purging screenshots"); } }
/// <summary> /// Logs an exception raised in the task. Call this like the ContinueWith method with the /// <see cref="TaskContinuationOptions.OnlyOnFaulted"/> option. /// </summary> /// <typeparam name="TResult">The type of the result produced by this Task.</typeparam> /// <param name="task"></param> /// <param name="taskName">The name of the task, used for the exception log item context.</param> /// <returns>A new continuation <see cref="Task"/>.</returns> public static Task LogFaulted <TResult>(this Task <TResult> task, string taskName) { return(task.ContinueWith(t => FL.Error(t.Exception, taskName + " Task"), TaskContinuationOptions.OnlyOnFaulted)); }