Can we change the settings of the Windows applications app.config? - c#-4.0

I have two app.config's in my application where i have written two connectionstrings in one config file and another two in other config file .Now in .cs file i have written this
string Connect = ConfigurationManager.ConnectionStrings["con1"].ConnectionString.ToString();
string Connect = ConfigurationManager.ConnectionStrings["con3"].ConnectionString.ToString();
Now first line gives the connectionstring from app.config.But for the second line am getting an error that "object reference not set to an instance"
Can any one tell me how to use the connection from different app.config's

Per Microsoft's documentation for the ConfigurationManager class, the ConnectionStrings property operates on the current application's default configuration file.
I believe that what you probably want to do is use the OpenExeConfiguration method to read the second app.config file then use the returned Configuration class instead of the static ConfigurationManager.

Related

How can I copy an existing overthere.SshHost file in XL Deploy UI using Puppet?

The Infra team in my company has provided us with sample overthere.SshHost under 'Infrastructure' in XL-Deploy UI that has a predefined private key file and passphrase which is not shared with us.
We are asked to duplicate this file manually in the UI, rename it and create infra entries for our application.
How can I achieve this with puppet?
Lets say the sample file is placed under: Infrastructure/Project1/COMMONS/Template_SshHost
and I need to create an overthere.SshHost under Infrastructure/Project1/UAT/Uat_SshHost and Infrastructure/Project1/PREPROD/Preprod_SshHost by copying the sample file.
Thanks in advance!
You can sync a target file with another file accessible via the local file system by using a File resource whose source attribute specifies the path to the original. You can produce a modified copy in a variety of ways, such as by applying one or more File_line resources (from stdlib) or by applying an appropriate script via an Exec resource.
But if you go that route then you have to either
accept that the target file will be re-synced on every Puppet run, OR
set the File resource's replace attribute to false, in which case changes to the original file will not be propagated into the customized copy.
The latter is probably the more acceptable choice for most people. Its file-copying part might look something like this:
$project_dir = '/path/to/Infrastructure/Project1'
file { "${project_dir}/UAT/Uat_SshHost/overthere.SshHost":
ensure => 'file',
source => "${project_dir}/COMMONS/Template_SshHost/overthere.SshHost",
replace => false,
}
But you might want to consider instead writing a custom type and provider for the target file. That would allow you to incorporate changes from the original template without re-syncing the file on every run, and it would give you a lot more flexibility with respect to the customizations you need to apply. It would also present a simpler interface for you to use in your manifests, which could make managing these easier. But, of course, that's offset by the cost is that writing and maintaining a custom type and provider. Only you can determine whether that would be a worthwhile trade-off.

asp.net core 2.0 Configuration.GetConnectionString doesn't seem to be reading appsetting.environment.json

I can add a "ConnectionStrings" element to my appsettings.json file; however, when I copy and paste the same element in to my appsettings.Development.json file it doesn't seem to work. My var connection = Configuration.GetConnectionString("DefaultDatabase"); line of code no longer works within Startup.cs and the ConfigureServices() method.
I am using Visual Studio 2017 and when i go to the properties of the project and go to the Debug tab I can see that it is setting ASPNETCORE_ENVIRONMENT to Development so shouldn't the connection string within appsettings.Development.json work with the Conifugration.GetConnectionString() method?
Ugh ok apparently I was pasting the code in to the "Logging" section element (right after the LogLevel element. Now that I truly have ConnectionStrings as its own element instead of a property of Logging all is well now.

How can you specify the file name for a Log4net SmtpPickupDirAppender?

By default the SmtpPickupDirAppender appender creates a file without an extension. I can't find that there is a parameter (or other option) to configure the file extension.
The email service that picks up the file from the directory specified in the "pickupDir" parameter requires a file extension.
Is there a way to specify one?
Not built in.
From the source, it looks like it just creates a file name based on a guid and doesn't offer any configuration options,
filePath = Path.Combine(m_pickupDir, SystemInfo.NewGuid().ToString("N"));
writer = File.CreateText(filePath);
However, if you pull the source, it should be relatively straight forward to create your own appender that adds an file extension.

Can you set a property value within the log4net config?

I'm working on a C# application that will have quite a few log files. These log files will be created in the same file path, and this file path will be determined at configuration time.
Is there a way I could specify this log files basepath in a single location the log4net configuration e.g. in a property?
I'd, of course, be referencing this basepath in the file param of each logfile appender?
One option I've considered is setting this path in the app.config/web.config, which the application will transfer into a log4net global context property. However, that would mean making sure that all loggers are created AFTER this property is set.
Any comments on the preferred, or the fallback method are welcome.
cheers!
You can implement a log4net custom pattern converter to return the basepath for logfiles. This is explained here: https://stackoverflow.com/a/4389828/910348.
The difference for your scenario is that your converter can simply read the basepath from your app.config/web.config.
Cheers!

log4 net Dynamic file name assigning

I want to know how to dynamically assign file name using Log4net .My application is such that 10 different files should be dynamically created based on user input ,and later based on the name the corresponding file name needs to be picked up and information written to it
For example in my application based on my buisness requirement for every xml file a corresponding log file with the same name as xml file should be created .Later whenever I do any modification to the xml file an entry needs to be in the corresponding log file
Please help . I having trouble to get control of the appropriate log to write it
Have not done this, but there are probably a number of ways of doing this, so this may not be the best way, but it should work
public OpenLogFile(string fileName)
{
log4net.Layout.ILayout layout = new log4net.Layout.PatternLayout("%d [%t]%-5p : - %m%n");;
log4net.Appender.FileAppender appender = new log4net.Appender.FileAppender(layout , filename);
appender.Threshold = log4net.Core.Level.Info;
log4net.Config.BasicConfigurator.Configure(appender);
}
Then just call OpenLogfile when you need to switch files.
You might need to tweak the layout or appender type.
A big disadvantage of this method is you losing the xml configuration and the ability to change settings at runtime. So a better way might be to configure your appender in the xml file to use a property
eg
file type="log4net.Util.PatternString" value="Logfiles\Log_For_%property{MyLogFileName}"
Then in your code you could change the property
log4net.GlobalContext.Properties["MyLogFileName"] = ...;
The tricky bit is to get log4net to reload itself. I haven't read the documentation of this, so I don't know if there is a way of forcing a reload. It might work if you just call log4net.Config.XmlConfigurator.ConfigureAndWatch again. Otherwise it should work if you opened the xml file and saved it again (without needing to change anything)
Hope this helps.

Resources