log4net change conf file path dynamicaly - c#

I need to change the conf file path depending on the environment.
In my Gobal.asaw.cs i have :
log4net.Config.XmlConfigurator.Configure();
In my web.config :
<add key="log4net.Config" value="conf/Log4netDev.config"/>
<add key="log4net.Config.Watch" value="True"/>
In my web.Debug.config :
<add key="log4net.Config" value="conf/Log4netDev.config" xdt:Locator="Match(key)" xdt:Transform="Replace" />
<add key="log4net.Config.Watch" value="True" xdt:Locator="Match(key)" xdt:Transform="Replace"/>
In my web.Release.config :
<add key="log4net.Config" value="conf/Log4netRelease.config" xdt:Locator="Match(key)" xdt:Transform="Replace" />
<add key="log4net.Config.Watch" value="True" xdt:Locator="Match(key)" xdt:Transform="Replace"/>
So in my web.config, the log4net.Config value is same as in web.debug.config
With visual studio, if i run in debug, i can find my logFile where it's supposed to be, everything is ok.
If i run in release, no log file...
I've tried to replace the log4net.Config value in web.config with the value i have in web.release.config, and now : when i run project in Release i have my log file and not in debug anymore.
What i understand is this only work when the value is in the web.config

Why not change it programmatically ?
var loggingConfigurationFile = HttpContext.Current.IsDebuggingEnabled ? "conf/Log4netDev.config" : "conf/Log4netRelease.config";
log4net.Config.XmlConfigurator.Configure(new FileInfo(loggingConfigurationFile));

Related

ASP.NET MVC - Change appsettings tag location from webConfig

I will need to remove the appsettings tag from the webconfig and put it elsewhere, but how do I find this new location in the code?
My app settings:
<appSettings>
<add key="ClientValidationEnabled" value="true" />
<add key="TimeEditDeleteApont" value="180" />
<add key="TimeStockApont" value="180" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="AdminUserName" value="Admin#Admin.com" />
<add key="AdminPassword" value="Password#1" />
<add key="TempUploadFolder" value="~/content/upload/temp" />
<!--<add key="FinalUploadFolder" value="~/content/images/apontamento" />-->
<add key="AllowedFiles" value=".ppt,.pttx,.pdf,.jpeg,.jpg,.gif,.png,.doc,.docx,.xls,.xlsx" />
<add key="MaxFileSizeMegaByte" value="25" />
</appSettings>
How i use inside my code:
public BaseController()
{
ViewBag.FinalUploadFolder = System.Configuration.ConfigurationManager.AppSettings.Get("FinalUploadFolder");
ViewBag.TempUploadFolder = System.Configuration.ConfigurationManager.AppSettings.Get("TempUploadFolder");
}
public string TimeConfig
{
get
{
return ConfigurationManager.AppSettings["TimeEditDeleteApont"];
}
}
How can I change the location of appSettings and then find that location within the code?
After I create a config file at the root of the folder, how do I find it inside the code?
In case somebody needs to separate the <appSettings> from the webConfig file, I did it as follows:
I created a appSettings.config file at the root of the project and then at <appSettings> I pointed to this file as follows:
<appSettings
configSource="appSettings.config">
</appSettings>

Config reads value from another config

I am trying to setup configs so it is easy to switch from dev to production with out constantly having to copy config settings from one place to another.
What I am trying to do is to setup a singe config that contains usernames, passwords and other access strings that I want to omit from source control for obvious reasons.
I could then reference those keys in other configs.
An example of what I am thinking
Content of a config that stores keys:
<keys>
<add key="username" value="1forest1" />
<add key="password" value="life-is-like-a-box-of-chocolates" />
<add key="url" value="http://www.example.com" />
</keys>
Content of a config that requires keys:
<service name="SomeService">
<settings>
<setting key="Container" value="MyContainer" />
<setting key="MaxBytes" value="12582912" />
<setting key="Timeout" value="30000" />
<setting key="Host" value=[url value from keys config] />
<setting key="username" value=[username value from keys config] />
<setting key="password" value=[password value from keys config] />
</settings>
</service>
I am not to optimistic that this is possible due to reasons, but any patterns that might point towards a solution/workaround for this would greatly appreciated.
Check out the Microsoft.Extensions.Configuration tools. Adding json files.
IConfiguration configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("Secrets/values.json")
.Build();
Inject the Configuration then get the values:
var section = configuration.GetSection("Settings");
string connectionString = section["ConnectionString"]
json example:
{
"Settings": {
"ConnectionString": "Server=tcp.....",
"SomeOtherValue": "SomeValue"
},
"MoreStuff": {
"foo": "bar"
}
}
Have an appsettings file:
Web.config:
<appSettings file="AppSettingsSecrets.config">
<add key="Container" value="MyContainer" />
<add key="MaxBytes" value="12582912" />
<add key="Timeout" value="30000" />
<add key="Host" value="" />
<add key="username" value="" />
<add key="password" value="" />
</appSettings>
AppSettingsSecrets.config :
<?xml version="1.0"?>
<appSettings>
<add key="Host" value="SecretHost" />
<add key="username" value="MyUname" />
<add key="password" value="S3cr3t" />
</appSettings>
Give the location/name of the second config file as one setting in first config file, and read it dynamically.

Multiple web config files in ASP.NET

Well, yes there are multiple posts on the same subject. One of the solutions for my problem is multiple web.config files but I am not sure if it works.
The problem:
I have a asp.net project. I have two clients (having their own storage and database) on which i need the application to be deployed. Storage and database are just two examples but there are many other settings unique to the client which can be managed in app settings. Whatever changes i do the project code, i need to deploy for both the clients.
Currently my web config looks like this:
<!-- GHR Settings -->
<connectionStrings>
<add name="DefaultConnection" connectionString="conn-string" providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=<acntName>;AccountKey=<acntKey>" />
<add key="StorageURL" value="<storageurl>" />
<add key="ProfileURL" value="<ProfileURL>" />
<add key="GenericURL" value="<GenericURL>" />
<add key="IDocURL" value="<IDocURL>" />
<add key="LogosURL" value="<LogosURL>" />
<add key="DocsURL" value="<DocsURL>" />
<add key="DefaultPassword" value="pass123" />
</appSettings>
<!-- TP Settings -->
<!--
<connectionStrings>
<add name="DefaultConnection" connectionString="conn-string" providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=<acntName>;AccountKey=<acntKey>" />
<add key="StorageURL" value="<storageurl>" />
<add key="ProfileURL" value="<ProfileURL>" />
<add key="GenericURL" value="<GenericURL>" />
<add key="IDocURL" value="<IDocURL>" />
<add key="LogosURL" value="<LogosURL>" />
<add key="DocsURL" value="<DocsURL>" />
<add key="DefaultPassword" value="pass123" />
</appSettings>
<add key="DefaultPassword" value="pass123" />
</appSettings> -->
As you can see, I have duplicated the settings and comment one client's settings, deploy on the server. Then I do it for other client.
This works alright, but too much maintenance during the publishing and prone to errors.
Please suggest what is the correct way of doing this.
Thanks.
What I have done in my case is create a separate config file for the DB connectionstring and reference that file inside your web.config. By this way you can have the same web.config for both your clients and would only need to send the connectionstring.config file only once!
Inside your web.config
<connectionStrings configSource="ConfigFiles\ConnectionStrings.config" />
And inside that file put the connectionstring
Same thing can be done for section

WebConfig Merge AppSettings

The situation is this, I want to have a key in my Web.config that gets changed in my Web.Debug.config and my Web.Release.config but I am unsure as to how to accomplish this.
I have my Web.config:
<appSettings>
<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>
I'm not sure what my Web.Debug.config is meant to be to add a key key="host" value="somevalue", so I've tried this:
<appSettings>
<add key="RunningHost" value="http://localhost:55169/" xdt:Transform="Insert" />
</appSettings>
This:
<appSettings xdt:Transform="Insert">
<add key="RunningHost" value="http://localhost:55169/" />
</appSettings>
This:
<appSettings xdt:Transform="Replace">
<add key="RunningHost" value="http://localhost:55169/" />
</appSettings>
To no avail. I read my config like so:
var appSetting = WebConfigurationManager.AppSettings["RunningHost"];
So I found out the answer for this.
It's because when I debugged the web project it didn't compile a new config combining the web.debug.config with the web.config.
However, if you publish it for release then it will combine the web.release.config with the web.config.
So the simple fix was to put the RunningHost in the web.config. Then have the release override it!
The first option is correct. Your full Web.Debug.config should look something like this:
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="RunningHost" value="http://localhost:55169/" xdt:Transform="Insert" />
</appSettings>
</configuration>
You can right-click on the Web.Debug.config in Solution Explorer and choose Preview Transform to see the transformed output.

How to change the value of attribute in appSettings section with Web.config transformation

Is it possible to transform the following Web.config appSettings file:
<appSettings>
<add key="developmentModeUserId" value="00297022" />
<add key="developmentMode" value="true" />
/* other settings here that should stay */
</appSettings>
into something like this:
<appSettings>
<add key="developmentMode" value="false" />
/* other settings here that should stay */
</appSettings>
So, I need to remove the key developmentModeUserId, and I need to replace the value for the key developmentMode.
You want something like:
<appSettings>
<add key="developmentModeUserId" xdt:Transform="Remove" xdt:Locator="Match(key)"/>
<add key="developmentMode" value="false" xdt:Transform="SetAttributes"
xdt:Locator="Match(key)"/>
</appSettings>
See Also: Web.config Transformation Syntax for Web Application Project Deployment
Replacing all AppSettings
This is the overkill case where you just want to replace an entire section of the web.config. In this case I will replace all AppSettings in the web.config will new settings in web.release.config. This is my baseline web.config appSettings:
<appSettings>
<add key="KeyA" value="ValA"/>
<add key="KeyB" value="ValB"/>
</appSettings>
Now in my web.release.config file, I am going to create a appSettings section except I will include the attribute xdt:Transform=”Replace” since I want to just replace the entire element. I did not have to use xdt:Locator because there is nothing to locate – I just want to wipe the slate clean and replace everything.
<appSettings xdt:Transform="Replace">
<add key="ProdKeyA" value="ProdValA"/>
<add key="ProdKeyB" value="ProdValB"/>
<add key="ProdKeyC" value="ProdValC"/>
</appSettings>
Note that in the web.release.config file my appSettings section has three keys instead of two, and the keys aren’t even the same. Now let’s look at the generated web.config file what happens when we publish:
<appSettings>
<add key="ProdKeyA" value="ProdValA"/>
<add key="ProdKeyB" value="ProdValB"/>
<add key="ProdKeyC" value="ProdValC"/>
</appSettings>
Just as we expected – the web.config appSettings were completely replaced by the values in web.release config. That was easy!
If you want to make transformation your app setting from web config file to web.Release.config,you have to do the following steps.
Let your web.config app setting file is this-
<appSettings>
<add key ="K1" value="Debendra Dash"/>
</appSettings>
Now here is the web.Release.config for the transformation.
<appSettings>
<add key="K1" value="value dynamicly from Realease"
xdt:Transform="SetAttributes"
xdt:Locator="Match(key)"
/>
</appSettings>
This will transform the value of K1 to the new value in realese Mode.
I do not like transformations to have any more info than needed. So instead of restating the keys, I simply state the condition and intention. It is much easier to see the intention when done like this, at least IMO. Also, I try and put all the xdt attributes first to indicate to the reader, these are transformations and not new things being defined.
<appSettings>
<add xdt:Locator="Condition(#key='developmentModeUserId')" xdt:Transform="Remove" />
<add xdt:Locator="Condition(#key='developmentMode')" xdt:Transform="SetAttributes"
value="false"/>
</appSettings>
In the above it is much easier to see that the first one is removing the element. The 2nd one is setting attributes. It will set/replace any attributes you define here. In this case it will simply set value to false.

Categories

Resources