I have a solution with a bunch of .net framework applications that are all configured to use Seq for logging. Currently I have the same settings duplicated in every app.config file:
<appSettings>
<add key="serilog:using:Seq" value="Serilog.Sinks.Seq" />
<add key="serilog:write-to:Seq.serverUrl" value="http://localhost:5341" />
</appSettings>
If I would like to be able to configure the Seq URL for all the applications from a single file, not only when running in Visual Studio but also when the system has been deployed.
How can this be done?
I have looked into creating a common settings file and include it in all applications using <appSettings file="CommonSettings.config">, but from what I understand this actually creates a copy of the file for each application.
Related
.net C# visual studio
In our application we are using a single config file that has all the entries to our application
specific.
Now for release to production I need to add some extra entries to config file which should be just release
specific and not needed in development.
If it was just connection strings I could have added
`<connectionStrings configSource="Configuration\ConnectionStrings.config">
</connectionStrings>`
But I have some other entries like for example:
`<system.web>
<authorization />
<machineKey validationKey=""/>
</system.web>
<system.webServer>
<modules>
</modules>
</system.webServer>`
and some more.
So how can I configure my application to use one config file for dev and separate for production.
Thanks
There are a lot of similar questions and I have looked at every one I could find, to no avail.
I'm storing API keys for Google+ authentication in a .config file outside of my solution (in the same level as the solution folder).
I'm attempting to read the values back in Startup.Auth.cs, like so:
public void ConfigureAuth(IAppBuilder app)
{
app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
{
ClientId = WebConfigurationManager.AppSettings.Get("GoogleClientId"),
ClientSecret = WebConfigurationManager.AppSettings.Get("GoogleClientSecret")
});
}
Root Web.config:
<appSettings file="..\Secrets.config"> <!-- Path is correct, relative to Web.config -->
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
Secrets.config:
<configuration>
<appSettings>
<add key="GoogleClientId" value="shh" />
<add key="GoogleClientSecret" value="shh" />
</appSettings>
</configuration>
in a .config file outside of my solution (in the same level as the solution folder).
An IIS application is totally unaware of any folders outside of its virtual application folder. There is no "at the same level as a solution file" in the context of a web application because the solution file is not deployed with it.
If you want to put appSettings outside of your application folder, your only built-in choices are the root web.config file, or machine.config file, which are both global to the machine (but specific to the .NET framework version you are running on). See ASP.NET Configuration File Hierarchy and Inheritance.
But just for the record, it is easiest to manage in the long run if you keep application settings in your application's web.config file. Eventually, you will need to change to/add a new web server and you might be scratching your head for a while trying to work out why the settings no longer work when that time comes if they need to be placed outside of your virtual application folder.
Remove the <configuration>...</configuration> tag from Secrets.config
Secrets.config:
<appSettings>
<add key="GoogleClientId" value="shh" />
<add key="GoogleClientSecret" value="shh" />
</appSettings>
Basically you make the appSettingssection the root of the external Secrets.config file.
The same can be done for connection strings using the configSource attribute except that...
Security - Unlike the Secrets.config file, the external connection
strings file must be in the same directory as the root web.config
file, so you'll have to take precautions to ensure you don't check it
into your source repository.
Also try using the ConfigurationManager
app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
{
ClientId = ConfigurationManager.AppSettings["GoogleClientId"],
ClientSecret = ConfigurationManager.AppSettings["GoogleClientSecret"]
});
Referencing articles where I learned about it
Scott Hanselman
Best practices for private config data and connection strings in configuration in ASP.NET and Azure
Official MS documentation
Best practices for deploying passwords and other sensitive data to ASP.NET and Azure App Service
Did you try to verify that the secrets.config is copied after the build?
Verify that by right clicking on the file and viewing the properties that the build action of the file is set to copy always.
How can I set a different value when I publish my MVC5 do production server?
Example:
In dev I have
<appSettings>
<add key="XLSFile" value="C:\\temp\\file.xls" />
</appSettings>
And when I publish the project I want to set a different path:
<appSettings>
<add key="XLSFile" value="C:\\projectname\\file.xls" />
</appSettings>
You'll want to use a config file transformation. Essentially you will override the config file for your release build.
For example:
<add key="XLSFile" value="C:\\projectname\\file.xls" xdt:Transform="Replace" xdt:Locator="Match(key)"/>
You can use two config files one with name web.Debug.config and second web.Release.config. First one for development and second for production server when published and now specify different value of key in both files as required
I have a website with a connection string listed in its web.config. The connection string is altered by the Publish feature so that it can reference a development database until it is released, when it references a separate release database.
The website accesses the databases through some assemblies, though. They are Class Libraries so they can't be Published, at least as far as I can tell. I read that the web.config would override the app.config connectionstrings, but that doesn't seem to be happening.
Whenever I Publish the release site references the development database, unless I alter the assemblies app.config files to reference the release database.
I don't want to have to remember to do that every time. How do I handle this?
You've got two issues here:
1. How to remember to publish the correct settings each time you publish:
One way to deploy such settings is by using web.config transformations in Visual Studio. This is pretty easy to set up and means that you do not have to remember to update the settings each time you publish.
As well as debug and release environments, you can also create transforms for "UAT", "Staging", "Beta" or whatever other configs you might need.
You might find these articles useful: here, here and here.
For example, here is a transform for a Release environment:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
<customErrors defaultRedirect="GenericError.htm"
mode="RemoteOnly" xdt:Transform="Replace">
<error statusCode="500" redirect="InternalError.htm"/>
</customErrors>
</system.web>
</configuration>
Above, you will see that the transform for Release mode sets the attributes of the MyDB connection string (xdt:Transform="SetAttributes"), removes the debug attribute from the compilation section and replaces the customErrors section with a new version.
It's a simple, yet very powerful technique.
2. How to get your assemblies to pick up the settings in the web.config
If your libraries have been written in the usual way, they should be retrieving their connection strings by simply accessing the [Web]ConfigurationManager.ConnectionStrings property. Like #Bob Horn says, they should then pick up the settings from the host process's config file (in this case the web.config of your web app).
However, sometimes you might find that a library is getting its settings from a .Settings file in the project, in which case things get a little more complicated. You will need to copy the settings section of the app.config in to the web.config. (You can also do this using the transforms technology described above.)
If you have access to the source code of the other assemblies, find the part of the code that retrieves connection strings. If it's not accessing the ConfigurationManager class, then that might explain why it's not picking up the web.config file.
I have more than one solution projects for an application with using one app.config for each solution.
Can i do a separate configuration file (other than app.config) for common setting like db name (connection string) ?
Because currently i put these setting in each and every app.config file.
Please try following.
Create one config called "common.config" like below which will be common to all solutions and let's say it's located # "E:/myconfig". Please keep all common setting in this config.
<appSettings>
<add key="connstring" value="conn string value" />
</appSettings>
Now link this common config in you specific solution config lets say web.config using file attribute
<configuration>
<appSettings file="E:\myconfig\Common.config">
<add key="key1" value="value" />
</appSettings>
</configuration>
Hope this will work for you.