/// <summary> /// Creates a new name value collection and overrides its values /// with system values (environment variables). /// </summary> /// <param name="props">The base properties to override.</param> /// <returns>A new NameValueCollection instance.</returns> private static NameValueCollection OverrideWithSysProps(NameValueCollection props) { NameValueCollection retValue = new NameValueCollection(props); IDictionary <string, string> vars = QuartzEnvironment.GetEnvironmentVariables(); foreach (string key in vars.Keys) { retValue.Set(key, vars[key]); } return(retValue); }
internal static NameValueCollection?InitializeProperties(ILog logger, bool throwOnProblem) { var props = Util.Configuration.GetSection(ConfigurationSectionName); var requestedFile = QuartzEnvironment.GetEnvironmentVariable(PropertiesFile); string propFileName = !string.IsNullOrWhiteSpace(requestedFile) ? requestedFile : "~/quartz.config"; // check for specials propFileName = FileUtil.ResolveFile(propFileName) ?? "quartz.config"; if (props == null && File.Exists(propFileName)) { // file system try { PropertiesParser pp = PropertiesParser.ReadFromFileResource(propFileName !); props = pp.UnderlyingProperties; logger?.Info($"Quartz.NET properties loaded from configuration file '{propFileName}'"); } catch (Exception ex) { logger?.ErrorException("Could not load properties for Quartz from file {0}: {1}".FormatInvariant(propFileName !, ex.Message), ex); } } if (props == null) { // read from assembly try { PropertiesParser pp = PropertiesParser.ReadFromEmbeddedAssemblyResource("Quartz.quartz.config"); props = pp.UnderlyingProperties; logger?.Info("Default Quartz.NET properties loaded from embedded resource file"); } catch (Exception ex) { logger?.ErrorException("Could not load default properties for Quartz from Quartz assembly: {0}".FormatInvariant(ex.Message), ex); } } if (props == null && throwOnProblem) { throw new SchedulerConfigException( @"Could not find <quartz> configuration section from your application config or load default configuration from assembly. Please add configuration to your application config file to correctly initialize Quartz."); } return(props); }
/// <summary> /// Read the Quartz.Net configuration from the config file /// </summary> /// <returns></returns> public NameValueCollection GetProperties() { try { var quartzFile = ConfigurationManager.AppSettings["QuartzFile"].Trim(' '); var requestedFile = QuartzEnvironment.GetEnvironmentVariable(quartzFile); var propFileName = !string.IsNullOrWhiteSpace(requestedFile) ? requestedFile : $"~/{quartzFile}"; propFileName = FileUtil.ResolveFile(propFileName); var propertiesParser = PropertiesParser.ReadFromFileResource(propFileName); var properties = propertiesParser.UnderlyingProperties; return(properties); } catch (Exception e) { _logger.Error($"Message:ServiceRunner Reading the Quartz config file failed,Error Message:{e.Message},DateTime:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}", e); } return(new NameValueCollection()); }
/// <summary> /// Initialize the <see cref="ISchedulerFactory" />. /// </summary> /// <remarks> /// By default a properties file named "quartz.properties" is loaded from /// the 'current working directory'. If that fails, then the /// "quartz.properties" file located (as an embedded resource) in the Quartz.NET /// assembly is loaded. If you wish to use a file other than these defaults, /// you must define the system property 'quartz.properties' to point to /// the file you want. /// </remarks> public void Initialize() { // short-circuit if already initialized if (cfg != null) { return; } if (initException != null) { throw initException; } NameValueCollection props = (NameValueCollection)ConfigurationManager.GetSection(ConfigurationSectionName); string requestedFile = QuartzEnvironment.GetEnvironmentVariable(PropertiesFile); string propFileName = requestedFile != null && requestedFile.Trim().Length > 0 ? requestedFile : "~/quartz.config"; // check for specials try { propFileName = FileUtil.ResolveFile(propFileName); } catch (SecurityException) { log.WarnFormat("Unable to resolve file path '{0}' due to security exception, probably running under medium trust"); propFileName = "quartz.config"; } if (props == null && File.Exists(propFileName)) { // file system try { PropertiesParser pp = PropertiesParser.ReadFromFileResource(propFileName); props = pp.UnderlyingProperties; Log.Info(string.Format("Quartz.NET properties loaded from configuration file '{0}'", propFileName)); } catch (Exception ex) { Log.Error("Could not load properties for Quartz from file {0}: {1}".FormatInvariant(propFileName, ex.Message), ex); } } if (props == null) { // read from assembly try { PropertiesParser pp = PropertiesParser.ReadFromEmbeddedAssemblyResource("Quartz.quartz.config"); props = pp.UnderlyingProperties; Log.Info("Default Quartz.NET properties loaded from embedded resource file"); } catch (Exception ex) { Log.Error("Could not load default properties for Quartz from Quartz assembly: {0}".FormatInvariant(ex.Message), ex); } } if (props == null) { throw new SchedulerConfigException( @"Could not find <quartz> configuration section from your application config or load default configuration from assembly. Please add configuration to your application config file to correctly initialize Quartz."); } Initialize(OverrideWithSysProps(props)); }