예제 #1
0
 public static FileServicesOptions AddFileStore(this FileServicesOptions app, FileStorage.IFileStore fileStore)
 {
     return(AddFileStore(app, sp =>
     {
         return fileStore;
     }));
 }
        /// <summary>
        /// Writes the <paramref name="source"/> instance, of type <typeparamref name="T"/>, as binary data to the file with the given <paramref name="rootFilename"/> in the specified <paramref name="fileStore"/>.
        /// </summary>
        /// <typeparam name="T">The <see cref="Type"/> of the instance.</typeparam>
        /// <param name="source">The instance to save.</param>
        /// <param name="fileStore">The <see cref="FileStorage.IFileStore"/> to save the file into.</param>
        /// <param name="rootFilename">The name of the file to save the binary data to.</param>
        public void Save <T>(T source, FileStorage.IFileStore fileStore, string rootFilename)
        {
            // check for errors
            if (string.IsNullOrWhiteSpace(rootFilename))
            {
                throw new ArgumentNullException(nameof(rootFilename));
            }
            if (fileStore == null)
            {
                throw new ArgumentNullException(nameof(fileStore));
            }
            // save T to file
            var tempFilename = System.IO.Path.GetTempFileName();

            try
            {
                // save file to temp file
                Save(source, tempFilename, true);
                // add temp file to file store
                var fileInfo = new System.IO.FileInfo(tempFilename);
                fileStore.Add(rootFilename, tempFilename, fileInfo.LastWriteTimeUtc, fileInfo.Length);
                fileStore.Save(false);
            }
            finally
            {
                // delete temp file
                if (System.IO.File.Exists(tempFilename))
                {
                    System.IO.File.Delete(tempFilename);
                }
            }
        }
예제 #3
0
        private IPackageConfiguration ExtractAndReadConfiguration(FileStorage.IFileStore fileStore,
                                                                  Configuration.IConfigurationSerializer <StringBuilder> serializer,
                                                                  string filename)
        {
            if (!fileStore.Contains(filename))
            {
                return(null);
            }
            var extractedFilename = fileStore.Get(filename).Extract(System.IO.Path.GetTempPath(), true);

            if (!System.IO.File.Exists(extractedFilename))
            {
                return(null);
            }
            try
            {
                var config = serializer.Deserialize(new StringBuilder(System.IO.File.ReadAllText(extractedFilename)));
                return((IPackageConfiguration)Core.Configuration.ConfigurationHelper.InstantiateTypeFromConfigurationGroup(typeof(PackageConfiguration), config));
            }
            catch
            {
                // swallow any exception; return null
                return(null);
            }
            finally
            {
                FileStorage.FileStorageHelper.DeleteFile(extractedFilename);
            }
        }
예제 #4
0
 public LogControllerThreadWorker(LogMarshal logMarshal,
                                  LogControllerSettings logSettings,
                                  Logging.ILogStatistics logStats,
                                  FileStorage.IFileStore archiveFileStore,
                                  IArchiveFilenameFactory archiveFilenameFactory)
 {
     _LogMarshal             = logMarshal;
     _LogSettings            = logSettings;
     _LogStats               = logStats;
     _ArchiveFileStore       = archiveFileStore;
     _ArchiveFilenameFactory = archiveFilenameFactory;
 }
예제 #5
0
 /// <summary>
 /// Will copy all logs, which returns <b>true</b> from the <paramref name="logEntryFilterPredicate"/> function, from the <paramref name="log"/> and write them to a text file with the <paramref name="filename"/> in the given <paramref name="fileStore"/>.
 /// </summary>
 /// <remarks>
 /// <code>
 /// // The following characters are encoded into, and decoded from, the following:
 /// //      Percent        (%)  %&lt;25&gt;
 /// //      Semicolon      (;)  %&lt;3B&gt;
 /// //      Double-Quote   (")  %&lt;22&gt;
 /// //      Newline        ( )  %&lt;0D&gt;
 /// </code>
 /// </remarks>
 /// <param name="log">The logger to read log from.</param>
 /// <param name="fileStore">The file store to write the log file to.</param>
 /// <param name="filename">The name of the file in the file store to write the logs to.</param>
 /// <param name="writeLogEntriesUsingLocalTime">Sets whether the date is written to the log relative to UTC or the local computer's timezone.</param>
 /// <param name="logEntryFilterPredicate">The <see cref="Func{T, TResult}"/> should return <b>true</b> to write the log to the archive file; return <b>false</b> to skip writing the log.</param>
 /// <returns>The number of logs written to the log file.</returns>
 public static int ArchiveLogs(ILogReader log,
                               FileStorage.IFileStore fileStore,
                               string filename,
                               bool writeLogEntriesUsingLocalTime,
                               Func <ILogEntry, bool> logEntryFilterPredicate)
 {
     return(ArchiveLogs(log,
                        fileStore,
                        filename,
                        writeLogEntriesUsingLocalTime,
                        false,
                        0,
                        0,
                        logEntryFilterPredicate));
 }
예제 #6
0
 /// <summary>
 /// Instantiates a new <see cref="PackageProvider"/> .
 /// </summary>
 /// <param name="packagesFileStore">The file store containing the package files.</param>
 /// <param name="configurationFilename">The filename of the configuration file in the packages.</param>
 /// <param name="configurationSerializer">The configuration serializer to use to serialize and deserialize configuration files.</param>
 /// <param name="packageFileStoreProvider">A file store provider which facilitates creating and connecting to packages.</param>
 public PackageProvider(FileStorage.IFileStore packagesFileStore,
                        string configurationFilename,
                        Configuration.IConfigurationSerializer <StringBuilder> configurationSerializer,
                        IPackageFileStoreProvider packageFileStoreProvider)
     : this()
 {
     _AllPackagesFileStore = packagesFileStore ?? throw new ArgumentNullException(nameof(packagesFileStore));
     if (string.IsNullOrWhiteSpace(configurationFilename))
     {
         throw new ArgumentNullException(nameof(configurationFilename));
     }
     _ConfigurationFilename   = configurationFilename;
     _ConfigurationSerializer = configurationSerializer ?? throw new ArgumentNullException(nameof(configurationSerializer));
     PackageFileStoreProvider = packageFileStoreProvider ?? throw new ArgumentNullException(nameof(packageFileStoreProvider));
 }
예제 #7
0
 /// <summary>
 /// Will copy all logs from the <paramref name="log"/> and write them to a text file with the <paramref name="filename"/> in the given <paramref name="fileStore"/>.
 /// </summary>
 /// <remarks>
 /// <code>
 /// // The following characters are encoded into, and decoded from, the following:
 /// //      Percent        (%)  %&lt;25&gt;
 /// //      Semicolon      (;)  %&lt;3B&gt;
 /// //      Double-Quote   (")  %&lt;22&gt;
 /// //      Newline        ( )  %&lt;0D&gt;
 /// </code>
 /// </remarks>
 /// <param name="log">The logger to read log from.</param>
 /// <param name="fileStore">The file store to write the log file to.</param>
 /// <param name="filename">The name of the file in the file store to write the logs to.</param>
 /// <param name="writeLogEntriesUsingLocalTime">Sets whether the date is written to the log relative to UTC or the local computer's timezone.</param>
 /// <param name="truncateLogArchive">Sets whether the <paramref name="fileStore"/> will be automatically truncated.</param>
 /// <param name="truncateLogArchiveMaximumFiles">The total number of files allowed in the <paramref name="fileStore"/> before auto truncation is performed.</param>
 /// <param name="truncateLogArchivePercentageToRemove">The percentage of log archive files to remove once <paramref name="truncateLogArchiveMaximumFiles"/> has been exceeded.</param>
 /// <returns>The number of logs written to the log file.</returns>
 public static int ArchiveLogs(ILogReader log,
                               FileStorage.IFileStore fileStore,
                               string filename,
                               bool writeLogEntriesUsingLocalTime,
                               bool truncateLogArchive,
                               int truncateLogArchiveMaximumFiles,
                               double truncateLogArchivePercentageToRemove)
 {
     return(ArchiveLogs(log,
                        fileStore,
                        filename,
                        writeLogEntriesUsingLocalTime,
                        truncateLogArchive,
                        truncateLogArchiveMaximumFiles,
                        truncateLogArchivePercentageToRemove,
                        new Func <ILogEntry, bool>((e) => { return true; })));
 }
예제 #8
0
 /// <summary>
 /// Will remove files from the <paramref name="fileStore"/> based on the parameters given.
 /// </summary>
 /// <param name="fileStore">The <paramref name="fileStore"/> to remove files from.</param>
 /// <param name="truncateLogArchiveMaximumFiles">The maximum files allowed to remain in the <paramref name="fileStore"/>.</param>
 /// <param name="truncateLogArchivePercentageToRemove">The percentage of files to remove from the <paramref name="fileStore"/>.</param>
 private static void TruncateArchiveFileStore(FileStorage.IFileStore fileStore, int truncateLogArchiveMaximumFiles, double truncateLogArchivePercentageToRemove)
 {
     if (fileStore.Count >= truncateLogArchiveMaximumFiles)
     {
         var removeCount = (int)(fileStore.Count * truncateLogArchivePercentageToRemove);
         if (removeCount > 0)
         {
             foreach (var item in from x in fileStore.GetAll()
                      orderby x.RootFileLastModifiedTimeUtc ascending
                      select x)
             {
                 fileStore.Delete(item.RootFilename);
                 if (--removeCount <= 0)
                 {
                     break;
                 }
             }
         }
     }
 }
예제 #9
0
 /// <summary>
 /// Instantiates a new <see cref="LogController"/>.
 /// </summary>
 /// <param name="log">The <see cref="Logging.ILog"/>-based logger used to write the actual logs.</param>
 /// <param name="logArchiveFileStore">A <see cref="FileStorage.IFileStore"/> when log archives will be created.</param>
 /// <param name="archiveFilenameFactory">A <see cref="IArchiveFilenameFactory"/> used to create archive log file names.</param>
 /// <param name="logControllerSettings">Settings to control the actions of the <see cref="ILogController"/>.</param>
 public LogController(Logging.ILog log,
                      FileStorage.IFileStore logArchiveFileStore,
                      IArchiveFilenameFactory archiveFilenameFactory,
                      LogControllerSettings logControllerSettings)
     : this()
 {
     if (log == null)
     {
         throw new ArgumentNullException(nameof(log));
     }
     // **** turn OFF auto-truncation if it is the known interface:{Logging.ITruncatable}
     if (log is Logging.ITruncatable)
     {
         (log as Logging.ITruncatable).AutoTruncateEnabled = false;
     }
     //
     _Log                    = log;
     Settings                = logControllerSettings ?? throw new ArgumentNullException(nameof(logControllerSettings));
     _LogArchiveFileStore    = logArchiveFileStore ?? throw new ArgumentNullException(nameof(logArchiveFileStore));
     _ArchiveFilenameFactory = archiveFilenameFactory ?? throw new ArgumentNullException(nameof(archiveFilenameFactory));
 }
 /// <summary>
 /// Instantiates a new connected package.
 /// </summary>
 /// <param name="packagesStore">The file store containing the package.</param>
 /// <param name="packageRootFilename">The package file name in the file store.</param>
 /// <param name="originalFilename">The file name for the package file store.</param>
 /// <param name="packFileStore">A file store connected to the package file store.</param>
 /// <param name="packConfiguration">The package configuration read from this package.</param>
 internal ConnectedPackage(FileStorage.IFileStore packagesStore,
                           string packageRootFilename,
                           string originalFilename,
                           FileStorage.IFileStore packFileStore,
                           IPackageConfiguration packConfiguration)
     : this()
 {
     _PackagesStore = packagesStore ?? throw new ArgumentNullException(nameof(packagesStore));
     if (string.IsNullOrWhiteSpace(packageRootFilename))
     {
         throw new ArgumentNullException(nameof(packageRootFilename));
     }
     _PackageRootFilename = packageRootFilename;
     if (string.IsNullOrWhiteSpace(originalFilename))
     {
         throw new ArgumentNullException(nameof(originalFilename));
     }
     _OriginalFilename    = originalFilename;
     _FileStore           = packFileStore ?? throw new ArgumentNullException(nameof(packFileStore));
     PackageConfiguration = packConfiguration ?? throw new ArgumentNullException(nameof(packConfiguration));
 }
        /// <summary>
        /// Reads the contents of the file, specified by the <paramref name="rootFilename"/>, from the <paramref name="fileStore"/> and converts it from binary data into an instance of type <typeparamref name="T"/>.
        /// </summary>
        /// <typeparam name="T">The type to convert the contents of the <paramref name="rootFilename"/> into.</typeparam>
        /// <param name="rootFilename">The name of the file to convert.</param>
        /// <param name="fileStore">The <see cref="FileStorage.IFileStore"/> to read the file from.</param>
        /// <returns>The contents of the file, specified by the <paramref name="rootFilename"/>, from the <paramref name="fileStore"/> and converted from binary data into an instance of type <typeparamref name="T"/>.</returns>
        public T Load <T>(string rootFilename, FileStorage.IFileStore fileStore)
        {
            // check for errors
            if (string.IsNullOrWhiteSpace(rootFilename))
            {
                throw new ArgumentNullException(nameof(rootFilename));
            }
            if (fileStore == null)
            {
                throw new ArgumentNullException(nameof(fileStore));
            }
            if (!fileStore.Contains(rootFilename))
            {
                throw new System.IO.FileNotFoundException(string.Format("File not found. Filename={0}", rootFilename), rootFilename);
            }
            //
            var results       = default(T);
            var fileStoreItem = fileStore.Get(rootFilename);
            var extractedFile = "";

            if (fileStoreItem != null)
            {
                try
                {
                    extractedFile = fileStore.Extract(System.IO.Path.GetTempPath(), fileStoreItem, true);
                    results       = Load <T>(extractedFile);
                }
                finally
                {
                    // delete temp file
                    if (System.IO.File.Exists(extractedFile))
                    {
                        System.IO.File.Delete(extractedFile);
                    }
                }
            }
            return(results);
        }
예제 #12
0
 /// <summary>
 /// Initializes an instance of the ArchivedLogs using the <paramref name="archiveFileStore"/> as the source of log files.
 /// </summary>
 /// <param name="archiveFileStore">The file store which provides the log files.</param>
 public ArchivedLogs(FileStorage.IFileStore archiveFileStore)
     : this()
 {
     ArchiveFileStore = archiveFileStore ?? throw new ArgumentNullException(nameof(archiveFileStore));
 }
예제 #13
0
        /// <summary>
        /// Will copy all logs, which returns <b>true</b> from the <paramref name="logEntryFilterPredicate"/> function, from the <paramref name="log"/> and write them to a text file with the <paramref name="filename"/> in the given <paramref name="fileStore"/>.
        /// </summary>
        /// <remarks>
        /// <code>
        /// // The following characters are encoded into, and decoded from, the following:
        /// //      Percent        (%)  %&lt;25&gt;
        /// //      Semicolon      (;)  %&lt;3B&gt;
        /// //      Double-Quote   (")  %&lt;22&gt;
        /// //      Newline        ( )  %&lt;0D&gt;
        /// </code>
        /// </remarks>
        /// <param name="log">The logger to read log from.</param>
        /// <param name="fileStore">The file store to write the log file to.</param>
        /// <param name="filename">The name of the file in the file store to write the logs to.</param>
        /// <param name="writeLogEntriesUsingLocalTime">Sets whether the date is written to the log relative to UTC or the local computer's timezone.</param>
        /// <param name="truncateLogArchive">Sets whether the <paramref name="fileStore"/> will be automatically truncated.</param>
        /// <param name="truncateLogArchiveMaximumFiles">The total number of files allowed in the <paramref name="fileStore"/> before auto truncation is performed.</param>
        /// <param name="truncateLogArchivePercentageToRemove">The percentage of log archive files to remove once <paramref name="truncateLogArchiveMaximumFiles"/> has been exceeded.</param>
        /// <param name="logEntryFilterPredicate">The <see cref="Func{T, TResult}"/> should return <b>true</b> to write the log to the archive file; return <b>false</b> to skip writing the log.</param>
        /// <returns>The number of logs written to the log file.</returns>
        public static int ArchiveLogs(ILogReader log,
                                      FileStorage.IFileStore fileStore,
                                      string filename,
                                      bool writeLogEntriesUsingLocalTime,
                                      bool truncateLogArchive,
                                      int truncateLogArchiveMaximumFiles,
                                      double truncateLogArchivePercentageToRemove,
                                      Func <ILogEntry, bool> logEntryFilterPredicate)
        {
            if (log == null)
            {
                throw new ArgumentNullException(nameof(log));
            }
            if (fileStore == null)
            {
                throw new ArgumentNullException(nameof(fileStore));
            }
            if (string.IsNullOrWhiteSpace(filename))
            {
                throw new ArgumentNullException(nameof(filename));
            }
            if ((truncateLogArchivePercentageToRemove < 0) || (truncateLogArchivePercentageToRemove > 1))
            {
                throw new ArgumentOutOfRangeException(nameof(truncateLogArchivePercentageToRemove), $"Parameter truncateLogArchivePercentageToRemove must be between 0.0 and 1.0. Value={truncateLogArchivePercentageToRemove}");
            }
            if (logEntryFilterPredicate == null)
            {
                throw new ArgumentNullException(nameof(logEntryFilterPredicate));
            }
            //
            var archivedLogs = 0;
            // copy logs from the logger to the temp file
            var tempFilename = System.IO.Path.Combine(System.IO.Path.GetTempPath(), Guid.NewGuid().ToString());

            lock (log.SyncObject)
            {
                using (var sw = new System.IO.StreamWriter(tempFilename, false))
                {
                    foreach (var entry in from x in log.Read()
                             orderby x.Timestamp ascending
                             select x)
                    {
                        if ((entry != null) && (logEntryFilterPredicate(entry)))
                        {
                            sw.WriteLine(FormatLogLine(entry, writeLogEntriesUsingLocalTime));
                            ++archivedLogs;
                        }
                    }
                }
            }
            if (archivedLogs > 0)
            {
                // copy temp file into FileStore
                fileStore.Add(filename, tempFilename, DateTime.UtcNow, 0);
                if (truncateLogArchive)
                {
                    TruncateArchiveFileStore(fileStore, truncateLogArchiveMaximumFiles, truncateLogArchivePercentageToRemove);
                }
                fileStore.Save(true);
            }
            // delete temp file
            System.IO.File.Delete(tempFilename);
            // return number of archived logs
            return(archivedLogs);
        }
예제 #14
0
 public FileManagerController(//IFileManagerProvider fileManager,
     FileStorage.IFileStore fileStore)
 {
     //this._fileManager = fileManager;
     this._fileStore = fileStore;
 }
예제 #15
0
 private IPackageConfiguration RetrieveConfiguration(FileStorage.IFileStore packageStore, string configurationFilename) => ExtractAndReadConfiguration(packageStore, _ConfigurationSerializer, configurationFilename);