public void Configure(ConfigSource config) { pfxFileName = config.GetString("pfxFile"); if (String.IsNullOrEmpty(pfxFileName)) { throw new ConfigurationException(config, "pfxFile"); } pfxFilePassword = config.GetString("pfxPassword"); }
private static IAppender GetFileAppender(string loggerName, ConfigSource config, Level level) { // Logging directory, string value = config.GetString("directory"); if (value == null) { return(null); } // Set a log directory, string f = value.Trim(); if (!Directory.Exists(f)) { Directory.CreateDirectory(f); } value = value.Replace("\\", "/"); if (!value.EndsWith("/")) { value = value + "/"; } string fileName = config.GetString("file", null); if (fileName == null) { fileName = "node.log"; } value = Path.GetFullPath(Path.Combine(value, fileName)); if (!File.Exists(value)) { File.Create(value); } // Output to the log file, RollingFileAppender appender = new RollingFileAppender(); appender.RollingStyle = RollingFileAppender.RollingMode.Size; appender.Name = loggerName; appender.File = value; appender.AppendToFile = true; ConfigPattern(appender, config); appender.Threshold = level; appender.StaticLogFileName = true; appender.LockingModel = new FileAppender.MinimalLock(); appender.ActivateOptions(); return(appender); }
public void SimpleProperties() { StringBuilder sb = new StringBuilder(); sb.AppendLine("test=passed"); sb.AppendLine("me=spaced value"); ConfigSource source = new ConfigSource(); source.LoadProperties(GetStream(sb)); Assert.AreEqual("test", source.Keys[0]); Assert.AreEqual("me", source.Keys[1]); Assert.AreEqual("passed", source.GetString("test")); Assert.AreEqual("spaced value", source.GetString("me")); }
public void Init(ConfigSource config) { string loggerName = config.Name; if (loggerName == null) { throw new InvalidOperationException(); } Level level = GetLogLevel(config); IAppender appender = null; string logOutput = config.GetString("output"); if (logOutput == null || logOutput.Equals("console", StringComparison.InvariantCultureIgnoreCase)) { appender = GetConsoleAppender(loggerName, config, level); } else if (logOutput.Equals("file")) { appender = GetFileAppender(loggerName, config, level); } if (appender != null) { BasicConfigurator.Configure(appender); log = log4net.LogManager.GetLogger(loggerName); } }
private static Level GetLogLevel(ConfigSource config) { ILoggerRepository repository = LoggerManager.GetRepository(Assembly.GetCallingAssembly()); Level level = Level.Off; string val = config.GetString("level"); if (val != null) { level = repository.LevelMap[val]; if (level == null) { int index = val.IndexOf(':'); if (index == -1) { throw new Exception("The 'level' configuration must specify a name and a numeric level."); } string levelName = val.Substring(0, index); string sLevelValue = val.Substring(index + 1); int levelValue; if (Int32.TryParse(sLevelValue, out levelValue)) { repository.LevelMap.Add(levelName, levelValue); } } } return(level); }
private object ConfigureComponent(ConfigSource config, Type componentType, Type defaultType) { object obj = null; if (config != null) { string typeString = config.GetString("type"); if (String.IsNullOrEmpty(typeString)) { if (defaultType != null) { obj = Activator.CreateInstance(defaultType); } } else { Type type = Type.GetType(typeString, false, true); if (type == null) { throw new ConfigurationException("The type '" + typeString + "' was not found in the current context.", config, "type"); } if (!componentType.IsAssignableFrom(type)) { throw new ConfigurationException("The type '" + type + "' is not an instance of '" + typeof(ITokenStore) + "'."); } try { obj = Activator.CreateInstance(type, null); } catch (Exception e) { throw new ConfigurationException("Unable to instantiate the type '" + type + "': " + e.Message, config, "type"); } } } else { if (defaultType != null) { obj = Activator.CreateInstance(defaultType); } } if (obj == null) { return(null); } if (obj is IRequiresProviderContext) { ((IRequiresProviderContext)obj).Context = this; } if (obj is IConfigurable) { ((IConfigurable)obj).Configure(config); } return(obj); }
public void Configure(ConfigSource configSource) { format = configSource.GetString("format"); if (String.IsNullOrEmpty(format)) { format = DefaultFormat; } else if (String.Compare(format, "N", true) != 0 && String.Compare(format, "D", true) != 0) { throw new ConfigurationException("Invalid or not supported GUID format specified", configSource, "format"); } }
private static IServiceFactory GetServiceFactory(string storage, ConfigSource nodeConfigSource) { if (storage == "file") { string nodeDir = nodeConfigSource.GetString("node_directory", Environment.CurrentDirectory); return(new FileSystemServiceFactory(nodeDir)); } if (storage == "memory") { return(new MemoryServiceFactory()); } if (String.IsNullOrEmpty(storage) && nodeConfigSource != null) { storage = nodeConfigSource.GetString("storage", "file"); return(GetServiceFactory(storage, nodeConfigSource)); } return(null); }
private static void ConfigPattern(AppenderSkeleton appender, ConfigSource config) { string logPattern = config.GetString("pattern", null); if (logPattern == null) { logPattern = "%date [%thread] %-5level %logger - %message%newline"; } PatternLayout layout = new PatternLayout(logPattern); layout.ActivateOptions(); appender.Layout = layout; }
public void Init(AdminService adminService) { if (storeType == StoreType.FileSystem) { ConfigSource config = adminService.Config; string basePath = config.GetString("node_directory", "./base"); factory = new FileSystemServiceFactory(basePath); } else { factory = new MemoryServiceFactory(); } factory.Init(adminService); }
private static void SetChildValues(Util.Properties properties, string prefix, ConfigSource config) { prefix += config.Name; foreach(string key in config.Keys) { string value = config.GetString(key, null); if (String.IsNullOrEmpty(value)) continue; properties.SetProperty(prefix + "." + key, value); } foreach(ConfigSource child in config.Children) { SetChildValues(properties, prefix, child); } }
private static void DoInit(string loggerName, ConfigSource config, List <ILogger> composites) { string loggerTypeString = config.GetString("type"); if (String.IsNullOrEmpty(loggerTypeString)) { loggerTypeString = "default"; } Type loggerType = GetLoggerType(loggerTypeString); if (loggerType == null || !typeof(ILogger).IsAssignableFrom(loggerType)) { return; } try { ILogger logger = (ILogger)Activator.CreateInstance(loggerType, true); if (logger is CompositeLogger) { if (composites == null) { return; } // composite loggers are processed with a later bound call composites.Add(logger); } else { logger.Init(config); } loggers[loggerName] = new Logger(loggerName, logger, config); } catch { return; } }
public void Configure(ConfigSource config) { consumerRequests = config.GetBoolean("consumerRequests"); ConfigSource child = config.GetChild("accessVerifier"); if (child != null) { string typeString = child.GetString("type"); if (String.IsNullOrEmpty(typeString)) { throw new ConfigurationException("The 'accessVerifier' type was not specified.", child, "type"); } Type type = Type.GetType(typeString, false, true); if (type == null) { throw new ConfigurationException("The type '" + typeString + "' could not be found.", child, "type"); } if (!typeof(IPathAccessVerifier).IsAssignableFrom(type)) { throw new ConfigurationException( "The type '" + type + "' cannot be is not an implementation of '" + typeof(IPathAccessVerifier) + "'.", child, "type"); } try { accessVerifier = (IPathAccessVerifier)Activator.CreateInstance(type); accessVerifier.Context = OAuthProvider.Current; accessVerifier.Configure(child); } catch (ConfigurationException) { throw; } catch (Exception e) { throw new ConfigurationException("Error while initializing the type '" + type + "': " + e.Message, e); } } }
private static IAppender GetConsoleAppender(string loggerName, ConfigSource config, Level level) { ConsoleAppender appender = new ConsoleAppender(); appender.Name = loggerName; appender.Threshold = level; string target = config.GetString("target"); if (target == "out" || target == "stdout" || target == null) { target = "Console.Out"; } if (target == "err" || target == "stderr") { target = "Console.Error"; } appender.Target = target; ConfigPattern(appender, config); appender.ActivateOptions(); return(appender); }
private static void DoInit(string loggerName, ConfigSource config, List<ILogger> composites) { string loggerTypeString = config.GetString("type"); if (String.IsNullOrEmpty(loggerTypeString)) loggerTypeString = "default"; Type loggerType = GetLoggerType(loggerTypeString); if (loggerType == null || !typeof(ILogger).IsAssignableFrom(loggerType)) return; try { ILogger logger = (ILogger)Activator.CreateInstance(loggerType, true); if (logger is CompositeLogger) { if (composites == null) return; // composite loggers are processed with a later bound call composites.Add(logger); } else { logger.Init(config); } loggers[loggerName] = new Logger(loggerName, logger, config); } catch { return; } }
void IAuthenticator.Init(ConfigSource config) { password = config.GetString("password"); }
public void TestString() { ConfigSource config = new ConfigSource(); config.SetValue("key", "value"); Assert.AreEqual("value", config.GetString("key")); }
private static int Main(string[] args) { string nodeConfig = null, netConfig = null; string hostArg = null, portArg = null; StringWriter wout = new StringWriter(); Options options = GetOptions(); CommandLine commandLine = null; bool failed = false; bool isService = false; try { ICommandLineParser parser = new GnuParser(options); commandLine = parser.Parse(args); nodeConfig = commandLine.GetOptionValue("nodeconfig", "./node.conf"); netConfig = commandLine.GetOptionValue("netconfig", "./network.conf"); hostArg = commandLine.GetOptionValue("host"); portArg = commandLine.GetOptionValue("port"); } catch (ParseException) { wout.WriteLine("Error parsing arguments."); failed = true; } if (commandLine != null) { if (commandLine.HasOption("install")) { try { Install(commandLine); Console.Out.WriteLine("Service installed succesfully."); return 0; } catch (Exception e) { Console.Error.WriteLine("Error installing service: " + e.Message); #if DEBUG Console.Error.WriteLine(e.StackTrace); #endif return 1; } } if (commandLine.HasOption("uninstall")) { try { Uninstall(); Console.Out.WriteLine("Service uninstalled succesfully."); return 0; } catch (Exception e) { Console.Error.WriteLine("Error uninstalling service: " + e.Message); #if DEBUG Console.Error.WriteLine(e.StackTrace); #endif return 1; } } isService = commandLine.HasOption("service"); } if (isService) { MachineNodeService mnodeService = new MachineNodeService(commandLine); try { if (Environment.UserInteractive) { mnodeService.Start(args); Console.Out.WriteLine("Press any key to stop..."); Console.Read(); mnodeService.Stop(); } else { ServiceBase.Run(mnodeService); } } catch(Exception) { return 1; } return 0; } AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(OnUnhandledException); SetEventHandlers(); ProductInfo libInfo = ProductInfo.GetProductInfo(typeof (TcpAdminService)); ProductInfo nodeInfo = ProductInfo.GetProductInfo(typeof (MachineNode)); Console.Out.WriteLine("{0} {1} ( {2} )", nodeInfo.Title, nodeInfo.Version, nodeInfo.Copyright); Console.Out.WriteLine(nodeInfo.Description); Console.Out.WriteLine(); Console.Out.WriteLine("{0} {1} ( {2} )", libInfo.Title, libInfo.Version, libInfo.Copyright); // Check arguments that can be null, if (netConfig == null) { wout.WriteLine("Error, no network configuration given."); failed = true; } else if (nodeConfig == null) { wout.WriteLine("Error, no node configuration file given."); failed = true; } if (portArg == null) { wout.WriteLine("Error, no port address given."); failed = true; } if (!failed) { //TODO: support for remote (eg. HTTP, FTP, TCP/IP) configurations) nodeConfig = NormalizeFilePath(nodeConfig); netConfig = NormalizeFilePath(netConfig); if (!File.Exists(nodeConfig)) { wout.WriteLine("Error, node configuration file not found ({0}).", nodeConfig); failed = true; } else if (!File.Exists(netConfig)) { wout.WriteLine("Error, node configuration file not found ({0}).", netConfig); failed = true; } } wout.Flush(); // If failed, if (failed) { HelpFormatter formatter = new HelpFormatter(); if (!IsConsoleRedirected()) { formatter.Width = Console.WindowWidth; } formatter.CommandLineSyntax = "mnode"; formatter.Options = options; formatter.PrintHelp(); Console.Out.WriteLine(); Console.Out.WriteLine(wout.ToString()); return 1; } try { #if DEBUG Console.Out.WriteLine("Retrieving node configuration from {0}", nodeConfig); #endif // Get the node configuration file, ConfigSource nodeConfigSource = new ConfigSource(); using (FileStream fin = new FileStream(nodeConfig, FileMode.Open, FileAccess.Read, FileShare.None)) { //TODO: make it configurable ... nodeConfigSource.LoadProperties(new BufferedStream(fin)); } #if DEBUG Console.Out.WriteLine("Retrieving network configuration from {0}", netConfig); #endif // Parse the network configuration string, NetworkConfigSource netConfigSource; using (FileStream stream = new FileStream(netConfig, FileMode.Open, FileAccess.Read, FileShare.None)) { netConfigSource = new NetworkConfigSource(); //TODO: make it configurable ... netConfigSource.LoadProperties(stream); } string password = nodeConfigSource.GetString("network_password", null); if (password == null) { Console.Out.WriteLine("Error: couldn't determine the network password."); return 1; } // configure the loggers Logger.Init(nodeConfigSource); //TODO: support also IPv6 // The base path, IPAddress host = null; if (hostArg != null) { IPAddress[] addresses = Dns.GetHostAddresses(hostArg); for (int i = 0; i < addresses.Length; i++) { IPAddress address = addresses[i]; if (address.AddressFamily == AddressFamily.InterNetwork) { host = address; break; } } } else { host = IPAddress.Loopback; } if (host == null) { Console.Out.WriteLine("Error: couldn't determine the host address."); return 1; } int port; if (!Int32.TryParse(portArg, out port)) { Console.Out.WriteLine("Error: couldn't parse port argument: " + portArg); return 1; } string storage = commandLine.GetOptionValue("storage", null); IServiceFactory serviceFactory = GetServiceFactory(storage, nodeConfigSource); Console.Out.WriteLine("Machine Node, " + host + " : " + port); service = new TcpAdminService(serviceFactory, host, port, password); service.Config = netConfigSource; service.Start(); waitHandle = new AutoResetEvent(false); waitHandle.WaitOne(); } catch (Exception e) { Console.Out.WriteLine(e.Message); Console.Out.WriteLine(e.StackTrace); return 1; } finally { if (service != null) service.Dispose(); } return 0; }
private static IServiceFactory GetServiceFactory(string storage, ConfigSource nodeConfigSource) { if (storage == "file") { string nodeDir = nodeConfigSource.GetString("node_directory", Environment.CurrentDirectory); return new FileSystemServiceFactory(nodeDir); } if (storage == "memory") return new MemoryServiceFactory(); if (String.IsNullOrEmpty(storage) && nodeConfigSource != null) { storage = nodeConfigSource.GetString("storage", "file"); return GetServiceFactory(storage, nodeConfigSource); } return null; }
private object ConfigureComponent(ConfigSource config, Type componentType, Type defaultType) { object obj = null; if (config != null) { string typeString = config.GetString("type"); if (String.IsNullOrEmpty(typeString)) { if (defaultType != null) obj = Activator.CreateInstance(defaultType); } else { Type type = Type.GetType(typeString, false, true); if (type == null) throw new ConfigurationException("The type '" + typeString + "' was not found in the current context.", config, "type"); if (!componentType.IsAssignableFrom(type)) throw new ConfigurationException("The type '" + type + "' is not an instance of '" + typeof(ITokenStore) + "'."); try { obj = Activator.CreateInstance(type, null); } catch (Exception e) { throw new ConfigurationException("Unable to instantiate the type '" + type + "': " + e.Message, config, "type"); } } } else { if (defaultType != null) obj = Activator.CreateInstance(defaultType); } if (obj == null) return null; if (obj is IRequiresProviderContext) ((IRequiresProviderContext)obj).Context = this; if (obj is IConfigurable) ((IConfigurable)obj).Configure(config); return obj; }
public void Configure(ConfigSource config) { pfxFileName = config.GetString("pfxFile"); if (String.IsNullOrEmpty(pfxFileName)) throw new ConfigurationException(config, "pfxFile"); pfxFilePassword = config.GetString("pfxPassword"); }
private static int Main(string[] args) { string nodeConfig = null, netConfig = null; string hostArg = null, portArg = null; StringWriter wout = new StringWriter(); Options options = GetOptions(); CommandLine commandLine = null; bool failed = false; bool isService = false; try { ICommandLineParser parser = new GnuParser(options); commandLine = parser.Parse(args); nodeConfig = commandLine.GetOptionValue("nodeconfig", "./node.conf"); netConfig = commandLine.GetOptionValue("netconfig", "./network.conf"); hostArg = commandLine.GetOptionValue("host"); portArg = commandLine.GetOptionValue("port"); } catch (ParseException) { wout.WriteLine("Error parsing arguments."); failed = true; } if (commandLine != null) { if (commandLine.HasOption("install")) { try { Install(commandLine); Console.Out.WriteLine("Service installed succesfully."); return(0); } catch (Exception e) { Console.Error.WriteLine("Error installing service: " + e.Message); #if DEBUG Console.Error.WriteLine(e.StackTrace); #endif return(1); } } if (commandLine.HasOption("uninstall")) { try { Uninstall(); Console.Out.WriteLine("Service uninstalled succesfully."); return(0); } catch (Exception e) { Console.Error.WriteLine("Error uninstalling service: " + e.Message); #if DEBUG Console.Error.WriteLine(e.StackTrace); #endif return(1); } } isService = commandLine.HasOption("service"); } if (isService) { MachineNodeService mnodeService = new MachineNodeService(commandLine); try { if (Environment.UserInteractive) { mnodeService.Start(args); Console.Out.WriteLine("Press any key to stop..."); Console.Read(); mnodeService.Stop(); } else { ServiceBase.Run(mnodeService); } } catch (Exception) { return(1); } return(0); } AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(OnUnhandledException); SetEventHandlers(); ProductInfo libInfo = ProductInfo.GetProductInfo(typeof(TcpAdminService)); ProductInfo nodeInfo = ProductInfo.GetProductInfo(typeof(MachineNode)); Console.Out.WriteLine("{0} {1} ( {2} )", nodeInfo.Title, nodeInfo.Version, nodeInfo.Copyright); Console.Out.WriteLine(nodeInfo.Description); Console.Out.WriteLine(); Console.Out.WriteLine("{0} {1} ( {2} )", libInfo.Title, libInfo.Version, libInfo.Copyright); // Check arguments that can be null, if (netConfig == null) { wout.WriteLine("Error, no network configuration given."); failed = true; } else if (nodeConfig == null) { wout.WriteLine("Error, no node configuration file given."); failed = true; } //if (portArg == null) { // wout.WriteLine("Error, no port address given."); // failed = true; //} if (!failed) { //TODO: support for remote (eg. HTTP, FTP, TCP/IP) configurations) nodeConfig = NormalizeFilePath(nodeConfig); netConfig = NormalizeFilePath(netConfig); if (!File.Exists(nodeConfig)) { wout.WriteLine("Error, node configuration file not found ({0}).", nodeConfig); failed = true; } else if (!File.Exists(netConfig)) { wout.WriteLine("Error, node configuration file not found ({0}).", netConfig); failed = true; } } wout.Flush(); // If failed, if (failed) { HelpFormatter formatter = new HelpFormatter(); if (!IsConsoleRedirected()) { formatter.Width = Console.WindowWidth; } formatter.CommandLineSyntax = "mnode"; formatter.Options = options; formatter.PrintHelp(); Console.Out.WriteLine(); Console.Out.WriteLine(wout.ToString()); return(1); } try { #if DEBUG Console.Out.WriteLine("Retrieving node configuration from {0}", nodeConfig); #endif // Get the node configuration file, ConfigSource nodeConfigSource = new ConfigSource(); using (FileStream fin = new FileStream(nodeConfig, FileMode.Open, FileAccess.Read, FileShare.None)) { //TODO: make it configurable ... nodeConfigSource.LoadProperties(new BufferedStream(fin)); } #if DEBUG Console.Out.WriteLine("Retrieving network configuration from {0}", netConfig); #endif // Parse the network configuration string, NetworkConfigSource netConfigSource; using (FileStream stream = new FileStream(netConfig, FileMode.Open, FileAccess.Read, FileShare.None)) { netConfigSource = new NetworkConfigSource(); //TODO: make it configurable ... netConfigSource.LoadProperties(stream); } string password = nodeConfigSource.GetString("network_password", null); if (password == null) { Console.Out.WriteLine("Error: couldn't determine the network password."); return(1); } // configure the loggers Logger.Init(nodeConfigSource); //TODO: support also IPv6 // The base path, IPAddress host = null; if (hostArg != null) { IPAddress[] addresses = Dns.GetHostAddresses(hostArg); for (int i = 0; i < addresses.Length; i++) { IPAddress address = addresses[i]; if (address.AddressFamily == AddressFamily.InterNetwork) { host = address; break; } } } else { host = IPAddress.Loopback; } if (host == null) { Console.Out.WriteLine("Error: couldn't determine the host address."); return(1); } int port = DefaultPort; if (!String.IsNullOrEmpty(portArg)) { if (!Int32.TryParse(portArg, out port)) { Console.Out.WriteLine("Error: couldn't parse port argument: " + portArg); return(1); } } string storage = commandLine.GetOptionValue("storage", null); IServiceFactory serviceFactory = GetServiceFactory(storage, nodeConfigSource); Console.Out.WriteLine("Machine Node, " + host + " : " + port); service = new TcpAdminService(serviceFactory, host, port, password); service.Config = netConfigSource; service.Start(); Console.Out.WriteLine(); Console.Out.WriteLine(); Console.Out.WriteLine("Press CTRL+C to quit..."); waitHandle = new AutoResetEvent(false); waitHandle.WaitOne(); } catch (Exception e) { Console.Out.WriteLine(e.Message); Console.Out.WriteLine(e.StackTrace); return(1); } finally { if (service != null) { service.Dispose(); } } return(0); }
public void Init(ConfigSource config) { string log_path_string = config.GetString("path"); string root_path_var = config.GetString("rootPath"); string read_only_access = config.GetString("readOnly"); string debug_logs = config.GetString("debugLogs"); bool read_only_bool = false; if (read_only_access != null) { read_only_bool = String.Compare(read_only_access, "enabled", true) == 0; } bool debug_logs_bool = true; if (debug_logs != null) { debug_logs_bool = String.Compare(debug_logs, "enabled", true) == 0; } // Conditions for not initializing a log directory; // 1. Read only access is enabled // 2. log_path is empty or not set if (debug_logs_bool && !read_only_bool && log_path_string != null && !log_path_string.Equals("")) { // First set up the debug information in this VM for the 'Debug' class. string log_path = ParseFileString(Environment.CurrentDirectory, root_path_var, log_path_string); // If the path doesn't exist the make it. if (!Directory.Exists(log_path)) Directory.CreateDirectory(log_path); LogWriter f_writer; String dlog_file_name = ""; try { dlog_file_name = config.GetString("file"); string debug_log_file = Path.Combine(Path.GetFullPath(log_path), dlog_file_name); // Allow log size to grow to 512k and allow 12 archives of the log //TODO: make it configurable... f_writer = new LogWriter(debug_log_file, 512 * 1024, 12); f_writer.Write("**** Debug log started: " + DateTime.Now + " ****\n"); f_writer.Flush(); } catch (IOException) { throw new Exception("Unable to open debug file '" + dlog_file_name + "' in path '" + log_path + "'"); } output = f_writer; } // If 'debug_logs=disabled', don't Write out any debug logs if (!debug_logs_bool) { // Otherwise set it up so the output from the logs goes to a PrintWriter // that doesn't do anything. Basically - this means all log information // will get sent into a black hole. output = new EmptyTextWriter(); } debug_level = config.GetInt32("level", -1); if (debug_level == -1) // stops all the output debug_level = 255; string format = config.GetString("format", null); if (format != null) message_format = format; }
public void Init(ConfigSource config) { string log_path_string = config.GetString("path"); string root_path_var = config.GetString("rootPath"); string read_only_access = config.GetString("readOnly"); string debug_logs = config.GetString("debugLogs"); bool read_only_bool = false; if (read_only_access != null) { read_only_bool = String.Compare(read_only_access, "enabled", true) == 0; } bool debug_logs_bool = true; if (debug_logs != null) { debug_logs_bool = String.Compare(debug_logs, "enabled", true) == 0; } // Conditions for not initializing a log directory; // 1. Read only access is enabled // 2. log_path is empty or not set if (debug_logs_bool && !read_only_bool && log_path_string != null && !log_path_string.Equals("")) { // First set up the debug information in this VM for the 'Debug' class. string log_path = ParseFileString(Environment.CurrentDirectory, root_path_var, log_path_string); // If the path doesn't exist the make it. if (!Directory.Exists(log_path)) { Directory.CreateDirectory(log_path); } LogWriter f_writer; String dlog_file_name = ""; try { dlog_file_name = config.GetString("file"); string debug_log_file = Path.Combine(Path.GetFullPath(log_path), dlog_file_name); // Allow log size to grow to 512k and allow 12 archives of the log //TODO: make it configurable... f_writer = new LogWriter(debug_log_file, 512 * 1024, 12); f_writer.Write("**** Debug log started: " + DateTime.Now + " ****\n"); f_writer.Flush(); } catch (IOException) { throw new Exception("Unable to open debug file '" + dlog_file_name + "' in path '" + log_path + "'"); } output = f_writer; } // If 'debug_logs=disabled', don't Write out any debug logs if (!debug_logs_bool) { // Otherwise set it up so the output from the logs goes to a PrintWriter // that doesn't do anything. Basically - this means all log information // will get sent into a black hole. output = new EmptyTextWriter(); } debug_level = config.GetInt32("level", -1); if (debug_level == -1) { // stops all the output debug_level = 255; } string format = config.GetString("format", null); if (format != null) { message_format = format; } }
protected override void OnStart(string[] args) { eventLog.WriteEntry("Starting the service.", EventLogEntryType.Information); string nodeConfig, netConfig; string hostArg, portArg; try { nodeConfig = commandLine.GetOptionValue("nodeconfig", "node.conf"); netConfig = commandLine.GetOptionValue("netconfig", "network.conf"); hostArg = commandLine.GetOptionValue("host"); portArg = commandLine.GetOptionValue("port"); } catch (ParseException e) { eventLog.WriteEntry("Parse Error: " + e.Message, EventLogEntryType.Error); ExitCode = 1; throw new ApplicationException("Invalid arguments passed."); } try { // Get the node configuration file, ConfigSource nodeConfigSource = new ConfigSource(); using (FileStream fin = new FileStream(nodeConfig, FileMode.Open, FileAccess.Read, FileShare.None)) { nodeConfigSource.LoadProperties(new BufferedStream(fin)); } // Parse the network configuration string, NetworkConfigSource netConfigSource; using (FileStream stream = new FileStream(netConfig, FileMode.Open, FileAccess.Read, FileShare.None)) { netConfigSource = new NetworkConfigSource(); //TODO: make it configurable ... netConfigSource.LoadProperties(stream); } string password = nodeConfigSource.GetString("network_password", null); if (password == null) { throw new ApplicationException("Error: couldn't determine the network password."); } // configure the loggers Logger.Init(nodeConfigSource); //TODO: support also IPv6 // The base path, IPAddress host = null; if (hostArg != null) { IPAddress[] addresses = Dns.GetHostAddresses(hostArg); for (int i = 0; i < addresses.Length; i++) { IPAddress address = addresses[i]; if (address.AddressFamily == AddressFamily.InterNetwork) { host = address; break; } } } else { host = IPAddress.Loopback; } if (host == null) { throw new ApplicationException("Could not determine the host address."); } int port; if (!Int32.TryParse(portArg, out port)) { throw new ApplicationException("Error: couldn't parse port argument: " + portArg); } string storage = commandLine.GetOptionValue("storage", null); IServiceFactory serviceFactory = GetServiceFactory(storage, nodeConfigSource); service = new TcpAdminService(serviceFactory, host, port, password); service.Config = netConfigSource; service.Start(); eventLog.WriteEntry("TCP/IP service started successfully: " + host + ":" + port, EventLogEntryType.Information); eventLog.WriteEntry("Storage system: " + storage); } catch (Exception e) { if (service != null) { service.Dispose(); } eventLog.WriteEntry("Error on start: " + e.Message, EventLogEntryType.Error); throw; } }