Ejemplo n.º 1
0
        /// <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);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Constructor.
        /// </summary>
        /// <param name="name">The name of the repository.</param>
        /// <param name="path">The path of the repository,</param>
        /// <param name="tokenProvider">The token provider.</param>
        /// <param name="eventLog">The event log.</param>
        /// <param name="cleanupInterval">Cleanup interval for the repository.</param>
        /// <param name="retentionPeriod">The retention period for the repository</param>
        public FileRepository(string name, string path, IFileTokenProvider tokenProvider, IEventLog eventLog, TimeSpan retentionPeriod, TimeSpan cleanupInterval)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (string.IsNullOrWhiteSpace(path))
            {
                throw new ArgumentNullException(nameof(path));
            }

            if (tokenProvider == null)
            {
                throw new ArgumentNullException(nameof(tokenProvider));
            }

            if (eventLog == null)
            {
                throw new ArgumentNullException(nameof(eventLog));
            }

            if (retentionPeriod.TotalMilliseconds <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(retentionPeriod));
            }

            if (cleanupInterval.TotalMilliseconds <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(cleanupInterval));
            }

            // Try and create the file repository path if it does not exist
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            eventLog.WriteInformation("Creating file repository {0} at {1}.", name, path);

            Name = name;

            _repositoryPath  = path.TrimEnd(Path.DirectorySeparatorChar);
            _tokenProvider   = tokenProvider;
            _eventLog        = eventLog;
            _cleanupInterval = cleanupInterval;
            _retentionPeriod = retentionPeriod;

            // Get the temp directory path
            _tempDirectoryPath = Path.Combine(path, TempDirectory);
            Directory.CreateDirectory(_tempDirectoryPath);
        }
Ejemplo n.º 3
0
        /// <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;
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 ///     Writes the information.
 /// </summary>
 /// <param name="message">The message.</param>
 /// <param name="args">The arguments.</param>
 public void WriteInformation(string message, params object[] args)
 {
     _view.WriteInformation(message, args);
 }