/// <summary> /// Runs the file cleanup as a background task. /// </summary> private void RunFileCleanup() { try { if (!ShouldRunFileCleanup()) { return; } lock (_syncRoot) { if (!ShouldRunFileCleanup()) { return; } _lastCleanupTimeUtc = DateTime.UtcNow; } Task.Run(() => { _eventLog.WriteInformation("Cleanup for file repository {0} starting", Name); var oldFiles = CleanupOldTempFiles(); var staleFiles = CleanupFilesExceedingRetentionPeriod(); var filesRemoved = FilesRemoved; if (filesRemoved != null && ((oldFiles != null && oldFiles.Count > 0) || (staleFiles != null && staleFiles.Count > 0))) { FilesRemovedEventArgs args = new FilesRemovedEventArgs(oldFiles, staleFiles); filesRemoved(this, args); } _eventLog.WriteInformation("Cleanup for file repository {0} completed.", Name); }).ContinueWith(t => { var cleanupComplete = CleanupComplete; cleanupComplete?.Invoke(this, new EventArgs( )); }); } catch (Exception exception) { _eventLog.WriteError("An error occured running the temp file cleanup for file repository {0}. Error {1}.", Name, exception); } }
/// <summary> /// Runs the actions and wait. /// </summary> private void RunActionsAndWait() { try { ConcurrentQueue <Action> rendezvousActions = Interlocked.Exchange(ref _rendezvousActions, new ConcurrentQueue <Action>()); // Run the registered actions and wait using (var cancellationTokenSource = new CancellationTokenSource()) { CancellationToken cancellationToken = cancellationTokenSource.Token; // Run all rendezvous actions asynchronously Task[] tasks = rendezvousActions.Select(action => Task.Run(() => RunAction(action, cancellationToken), cancellationToken)).ToArray(); if (!Task.WaitAll(tasks, _millisecondsTimeout, cancellationToken)) { cancellationTokenSource.Cancel(); } } } catch (Exception ex) { string msg = "An error occured waiting for rendezvous point actions to complete. Id:{0}, Error:{1}."; if (_eventLog != null) { _eventLog.WriteError(msg, _id, ex.ToString()); } else { Trace.TraceError(msg, _id, ex); } } }
/// <summary> /// Logs an error. /// </summary> /// <param name="message">The message.</param> /// <param name="ex">An exception.</param> public static void LogError(string message, Exception ex) { lock (_eventLog) { if (ex is System.Threading.ThreadAbortException) { _eventLog.WriteWarning("Task scheduling and execution aborted."); } else { var msg = string.Format("An error ocurred during task execution:\r\n\r\n{0}\r\n\r\n{1}: {2}\r\n\r\nStacktrace:\r\n{3}", message, ex.GetType().Name, ex.Message, ex.StackTrace); _eventLog.WriteError(msg); } } }
/// <summary> /// Writes the specified audit log entry. /// </summary> /// <param name="entryData">The entry data.</param> /// <exception cref="System.ArgumentNullException">entryData</exception> public void Write(IAuditLogEntryData entryData) { if (entryData == null) { throw new ArgumentNullException(nameof(entryData)); } // Writes an entry to the event log var eventLogMessage = new StringBuilder(); eventLogMessage.AppendLine($"Audit log entry. Type: '{entryData.AuditLogEntryType.Name}'"); eventLogMessage.AppendLine($"AuditLogMessage: '{entryData.Message}'"); eventLogMessage.AppendLine($"Success: '{entryData.Success}'"); eventLogMessage.AppendLine($"Severity: '{entryData.SeverityEnum}'"); eventLogMessage.AppendLine($"UserName: '******'"); eventLogMessage.AppendLine($"CreatedDate: '{entryData.CreatedDate.ToLocalTime().ToString("o")}'"); if (entryData.Parameters.Count > 0) { eventLogMessage.AppendLine("Parameters:"); foreach (var kvp in entryData.Parameters) { eventLogMessage.AppendLine($"{kvp.Key}: '{kvp.Value}'"); } } switch (entryData.SeverityEnum) { case AuditLogSeverityEnum_Enumeration.AuditLogError: _eventLog.WriteError(eventLogMessage.ToString()); break; case AuditLogSeverityEnum_Enumeration.AuditLogInformation: _eventLog.WriteInformation(eventLogMessage.ToString()); break; case AuditLogSeverityEnum_Enumeration.AuditLogWarning: _eventLog.WriteWarning(eventLogMessage.ToString()); break; } }
/// <summary> /// Writes the error. /// </summary> /// <param name="message">The message.</param> /// <param name="args">The arguments.</param> public void WriteError(string message, params object[] args) { _view.WriteError(message, args); }