Quartz .net configuration with code instead of config file - quartz.net

I am using Quartz .net with an AdoJobStore jobStore. In the .config file, there is a property that is used to set the connection string:
<add key="quartz.dataSource.default.connectionString" value="Data Source=MyServer;Initial Catalog=MyDatabase;Integrated Security=True" />
However, I manage all my connection strings in a separate config file, so I currently need to dupplicate this connectionString information.
Is there a way to not specify the quartz.dataSource.default.connectionString in the .config file and manually set it via code, so I can get my value in my global connectionStrings config file?
It seams that it is possible to use a NameValueCollection when instanciating a StdSchedulerFactory, but I don't want to manage all settings, only the connectionstring.

In the end, they are all simply named-value pairs.
You can have "most" of them in an .xml file...then "add in" the ones you want via code.
Or have all of them in code.
See the UnitTests for the source code, and you'll see this fairly clearly.
Something like this:
NameValueCollection config = (NameValueCollection)ConfigurationManager.GetSection("quartz");
config.Add("MyCodedUpKey", "MyCodedUpValue");
I would NOT put
"quartz.dataSource.default.connectionString"
in your .xml file...to avoid a collision..... Or you can write extra logic to remove and add to the
NameValueCollection .
Or take a peek here:
Check if Key Exists in NameValueCollection

Finally fix my problem by passing all the configuration attribute using code, but by parsing the quartz config to get them automatically.
And I manually add the quartz.dataSource.default.connectionString property at the end and setting the value by getting it in my global connectionStrings.config file.

Related

How to fall back to log4j2.xml when custom configurationFile is missing

My environment : JBoss EAP 7 with a "log4j2.xml" in classpath (historic behavior).
I would like to introduce a way to have a (non mandatory) custom log4j2 configuration file (per EAR application) but still use (fall back) to (existing) "log4j2.xml" if the custom configuration file is missing.
To me, the only way to accomplish this was to use composite configuration by using "log4j2.configurationFile" property (within log4j2.component.properties) and set both the "log4j2.xml" and the custom configuration filename (separated by a comma).
But if the custom file is missing, even the generic "log4j2.xml" is ignored.
When looking at log4j2 (v2.12.1) code (https://github.com/apache/logging-log4j2/blob/log4j-2.12.1/log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationFactory.java#L380) I can see that indeed if one config file is missing, none config file (of the list) is used (-> "return null")
Is there a way to accomplish the behavior I want?
Thanks

Controlling settings for serilog in app.config

I need to log events in Json format using Serilog. I want to use RollingFileSink.
According to the following link, we cannot specify TextFormatter in App.config file.
How to specify JsonFormatter in Web.config for SeriLog?
Is is possible to specify all other configurations like file path in app.config and then add textformatter as JsonFormatter in code while initialing the logger?
Just using plain settings works well for this, e.g.:
<add key="filePath" value="C:..." />
And:
WriteTo.RollingFile(..., ConfigurationManager.AppSettings["filePath"])

Get config value from file, or environment variable if file doesn't exist

I'm trying to get a setting from a configuration file (preferably something simple like .ini or JSON, not XML). If the file or setting does not exist, I want to be able to fall back to retrieving an environment variable.
I'd prefer to use an existing library for working with JSON/INI and not parsing the file myself. However, most libraries I've found won't work if a file doesn't exist.
How would I access a configuration value from a file that may or may not exist in F#?
You can use File.Exists to test whether or not the file exists:
open System.IO
let getConfig file =
if File.Exists file
then "config from file"
else "config from somewhere else"
OpenExeConfiguration (despite it's name) can open an arbitrary config file.
There's also the ASP.NET vNext Configuration stuff, outlined in this article which is quite flexible - no idea how separable (or relevant to your actual use case) it is [aside from the fact that you could conditionally include the config file into the config manager depending on whether it exists a la Mark's answer].
In addition to type providers, FSharp.Data provides some basic parsers, including JSON. This allows you to do a runtime check using File.Exists and then parse using your preferred utility.
I took the following approach in FAKE:
if File.Exists "local.json" then
let localVarProps = JsonValue.Parse(File.ReadAllText"local.json").Properties
for key, jsonValue in localVarProps do
setEnvironVar key (jsonValue.AsString())

Properties in the database

I'd like to store properties in a database tables and have defaults for those properties set in Config.groovy. For example, I want to put a default email address into Config.groovy:
app.send.report.to = 'me#example.com'
and then be able to override this in a database table (key, value columns...).
Is there a plugin (or functionality inside grails) to do this?
There is Dynamic Config Plugin.
It stores config property in ConfigProperty domain and merges properties from Config.groovy and from database using:
grailsApplication.config.merge(configObject)
You may want to look at the plugin source code. If plugin does not work for you, you can implement something similar to this.
This approach is useful when you have UI for editing config properties.
Grails does not have functionality that I'm aware of to override configuration values from a database, but it shouldn't be that difficult to do. In your Config.groovy you could put the defaults, and then as part of your bootstrap process, you could generate a temporary config file that has the values from the database (a simple query and iteration over the results could be used to generate that temp file). Include that temp file as one of your grails config locations, and it will override any values that are in the Config.groovy
If your goal is to have a shared configuration file that is used by multiple grails apps, you might also look into using something like Zookeeper to manage the shared configuration, but that may be a bit overkill for a single config file.
Not quite what you're asking for, but depending on what you want to achieve the External Configuration Reload plugin might be of use. It helps you to override default properties (in runtime), but not by using the DB.

Batch Size in web config for Nhibernate in ASP.NET MVC

How do I set the batch size for Nhibernate? I'd like to do this in the web.config. The examples I see, don't make a lot of sense to me.
In this example they are using code to set the batch size. I don't wnat to do that. I want it configurable in the web.config.
I understand how to add the config section, I just have read articles that differ on how to set this batch size. Most of them show batch size being set in code, and the ones that show it in the config use different names, or an appsettings key. Confusing.
When I add the config section into my web.config do I need to do anything special when creating the session or will nhibernate pick up the settings automatically?
in case anyone is wondering, I ended up just resorting to setting it up in my fluentnhibernate session factory.
Fluently.Configure().Database(
FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2005
.ConnectionString(c => c.FromConnectionStringWithKey("MyDatabase"))
.AdoNetBatchSize(20)
)
I also had to resort to using code to configure, though I only override the batch size parameter and otherwise use the Web.config settings as-is:
Configuration configuration = new Configuration();
configuration.SetProperty(Environment.BatchSize, "0");

Resources