From 402e7a5c028020768973df611fa7723b92b40bbf Mon Sep 17 00:00:00 2001 From: Eric Fontana Date: Mon, 4 Aug 2014 09:06:14 -0400 Subject: [PATCH] Added ability to read configuration .json files from a directory. --- TimberWinR.ServiceHost/Program.cs | 2 +- .../Properties/AssemblyInfo.cs | 4 +-- TimberWinR/Configuration.cs | 30 ++++++++++++++----- TimberWinR/Manager.cs | 29 ++++++++++++------ 4 files changed, 45 insertions(+), 20 deletions(-) diff --git a/TimberWinR.ServiceHost/Program.cs b/TimberWinR.ServiceHost/Program.cs index afadb46..56c953d 100644 --- a/TimberWinR.ServiceHost/Program.cs +++ b/TimberWinR.ServiceHost/Program.cs @@ -34,7 +34,7 @@ namespace TimberWinR.ServiceHost serviceConfigurator.WhenStarted(myService => myService.Start()); serviceConfigurator.WhenStopped(myService => myService.Stop()); }); - + hostConfigurator.AddCommandLineDefinition("configFile", c => arguments.ConfigFile = c); hostConfigurator.AddCommandLineDefinition("logLevel", c => arguments.LogLevel = c); diff --git a/TimberWinR.ServiceHost/Properties/AssemblyInfo.cs b/TimberWinR.ServiceHost/Properties/AssemblyInfo.cs index ac6f386..e384fa2 100644 --- a/TimberWinR.ServiceHost/Properties/AssemblyInfo.cs +++ b/TimberWinR.ServiceHost/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.1")] -[assembly: AssemblyFileVersion("1.0.0.1")] +[assembly: AssemblyVersion("1.1.0.0")] +[assembly: AssemblyFileVersion("1.1.0.0")] diff --git a/TimberWinR/Configuration.cs b/TimberWinR/Configuration.cs index 95bb90f..81c209f 100644 --- a/TimberWinR/Configuration.cs +++ b/TimberWinR/Configuration.cs @@ -79,24 +79,39 @@ namespace TimberWinR get { return _filters; } } - public static Configuration FromFile(string jsonConfFile) - { - Configuration c = new Configuration(); + public static Configuration FromDirectory(string jsonDirectory) + { + Configuration c = null; + + foreach (string jsonConfFile in Directory.GetFiles(jsonDirectory, "*.json")) + { + if (!string.IsNullOrEmpty(jsonConfFile)) + { + c = FromFile(jsonConfFile, c); + } + } + + return c; + } + + public static Configuration FromFile(string jsonConfFile, Configuration c = null) + { if (!string.IsNullOrEmpty(jsonConfFile)) { string json = File.ReadAllText(jsonConfFile); - return FromString(json); + return FromString(json, c); } return null; } - public static Configuration FromString(string json) + public static Configuration FromString(string json, Configuration c = null) { - Configuration c = new Configuration(); - + if (c == null) + c = new Configuration(); + JsonSerializer serializer = new JsonSerializer(); TextReader re = new StringReader(json); JsonTextReader reader = new JsonTextReader(re); @@ -126,7 +141,6 @@ namespace TimberWinR c._elasticsearchOutputs = x.TimberWinR.Outputs.Elasticsearch.ToList(); } - if (x.TimberWinR.Filters != null) c._filters = x.TimberWinR.AllFilters.ToList(); diff --git a/TimberWinR/Manager.cs b/TimberWinR/Manager.cs index 9dae491..ba37118 100644 --- a/TimberWinR/Manager.cs +++ b/TimberWinR/Manager.cs @@ -55,18 +55,29 @@ namespace TimberWinR LogManager.GlobalThreshold = LogLevel.FromString(logLevel); - var fi = new FileInfo(jsonConfigFile); - if (!fi.Exists) - throw new FileNotFoundException("Missing config file", jsonConfigFile); - - LogManager.GetCurrentClassLogger().Info("Initialized, Reading Config: {0}", fi.FullName); + + // Is it a directory? + if (Directory.Exists(jsonConfigFile)) + { + Config = Configuration.FromDirectory(jsonConfigFile); + LogManager.GetCurrentClassLogger().Info("Initialized, Reading Configurations From {0}", jsonConfigFile); + } + else + { + var fi = new FileInfo(jsonConfigFile); + + if (!fi.Exists) + throw new FileNotFoundException("Missing config file", jsonConfigFile); + + LogManager.GetCurrentClassLogger().Info("Initialized, Reading Config: {0}", fi.FullName); + Config = Configuration.FromFile(jsonConfigFile); + } + LogManager.GetCurrentClassLogger().Info("Log Directory {0}", logfileDir); - LogManager.GetCurrentClassLogger().Info("Logging Level: {0}", LogManager.GlobalThreshold); - + LogManager.GetCurrentClassLogger().Info("Logging Level: {0}", LogManager.GlobalThreshold); // Read the Configuration file - Config = Configuration.FromFile(jsonConfigFile); - + if (Config.RedisOutputs != null) { foreach (var ro in Config.RedisOutputs)