public OptionsManager() { var path = AppDomain.CurrentDomain.BaseDirectory; JSONPath = System.IO.Path.Combine(path, "appsettings.json"); XMLPath = System.IO.Path.Combine(path, "config.xml"); DefaultOptions = new ETLOptions(); Update(); Logger = new Logger(Options.LoggerOptions); }
protected void Copy(ETLOptions obj) { Path = obj.Path; TargetPath = obj.TargetPath; SourcePath = obj.SourcePath; ArchivePath = obj.ArchivePath; Copy(CrypterOptions, obj.CrypterOptions); Copy(ArchiverOptions, obj.ArchiverOptions); Copy(LoggerOptions, obj.LoggerOptions); }
public static void Validate(ETLOptions options) { WorkFoldersOptions workFoldersOptions = options.WorkFoldersOptions; if (!MakeValidDir(workFoldersOptions.SourceDir)) { workFoldersOptions.SourceDir = @"C:\Projects\FileWatcherService\source"; MakeValidDir(workFoldersOptions.SourceDir); Logger.Log("The access to source directory is denied, using default directory."); } if (!MakeValidDir(workFoldersOptions.TargetDir)) { workFoldersOptions.TargetDir = @"C:\Projects\FileWatcherService\target"; MakeValidDir(workFoldersOptions.TargetDir); Logger.Log("The access to target directory is denied, using default directory."); } LoggerOptions loggerOptions = options.LoggerOptions; if (!MakeValidFile(loggerOptions.LogFile)) { loggerOptions.LogFile = @"C:\Projects\FileWatcherService\log.txt"; MakeValidFile(loggerOptions.LogFile); Logger.Log("The access to log file is denied, using default log file."); } ArchivationOptions archivationOptions = options.ArchivationOptions; if (!MakeValidDir(archivationOptions.ArchiveDir)) { archivationOptions.ArchiveDir = @"C:\Projects\FileWatcherService\target\Archive"; MakeValidDir(archivationOptions.ArchiveDir); Logger.Log("The access to archive directory is denied, using default directory."); } if ((int)archivationOptions.CompressionLevel < 0 || (int)archivationOptions.CompressionLevel > 2) { archivationOptions.CompressionLevel = System.IO.Compression.CompressionLevel.Optimal; Logger.Log("Incorrect value of compression level. Default value is set."); } }
public Watcher() { string appDirectory = AppDomain.CurrentDomain.BaseDirectory; optionsManager = new OptionsManager(appDirectory); ETLOptions options = optionsManager.GetOptions <ETLOptions>() as ETLOptions; source = options.WorkFoldersOptions.SourceDir; target = options.WorkFoldersOptions.TargetDir; saveArchive = options.ArchivationOptions.ArchiveDir; Logger.loggerOptions = options.LoggerOptions; Logger.loaded = true; watcher = new FileSystemWatcher(source); watcher.Created += Created; }
Options.Options FindOption <T>(ETLOptions options) { if (typeof(T) == typeof(ETLOptions)) { return(options); } try { return(options.GetType().GetProperty(typeof(T).Name).GetValue(options, null) as Options.Options); } catch { Logger.Log("FindOption didn't find the needed option and throw a NotImplementedException."); throw new NotImplementedException(); } }
public OptionsManager(string path) { string options; try { using (StreamReader sr = new StreamReader($"{path}\\appsettings.json")) { options = sr.ReadToEnd(); } jsonOptions = new ETLJsonOptions(options); isJsonConfigured = true; Logger.Log("appsettings.json is loaded."); } catch (Exception ex) { isJsonConfigured = false; Logger.Log(ex.Message); } try { using (StreamReader sr = new StreamReader($"{path}\\config.xml")) { options = sr.ReadToEnd(); } xmlOptions = new ETLXmlOptions(options); isXmlConfigured = true; Logger.Log("config.xml is loaded."); } catch (Exception ex) { isXmlConfigured = false; Logger.Log(ex.Message); } if (!isJsonConfigured && !isXmlConfigured) { defaultOptions = new ETLOptions(); Logger.Log("Default options is used."); } }