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.
Related
If I set up my WCF project with an ApplicationInsights.config file as outlined in this Microsoft documentation, and data is logged to Application Insights (using the hardcoded instrumentation key) as expected.
Is there any way to specify instrumentation keys on a per-environment basis when using the ApplicationInsights.config file?
The config file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings">
<TelemetryInitializers>
<Add Type="Microsoft.ApplicationInsights.DependencyCollector.HttpDependenciesParsingTelemetryInitializer, Microsoft.AI.DependencyCollector" />
<Add Type="Microsoft.ApplicationInsights.WindowsServer.AzureRoleEnvironmentTelemetryInitializer, Microsoft.AI.WindowsServer" />
<Add Type="Microsoft.ApplicationInsights.WindowsServer.BuildInfoConfigComponentVersionTelemetryInitializer, Microsoft.AI.WindowsServer" />
<Add Type="Microsoft.ApplicationInsights.Web.WebTestTelemetryInitializer, Microsoft.AI.Web" />
<Add Type="Microsoft.ApplicationInsights.Web.SyntheticUserAgentTelemetryInitializer, Microsoft.AI.Web">
<!-- Extended list of bots:
search|spider|crawl|Bot|Monitor|BrowserMob|BingPreview|PagePeeker|WebThumb|URL2PNG|ZooShot|GomezA|Google SketchUp|Read Later|KTXN|KHTE|Keynote|Pingdom|AlwaysOn|zao|borg|oegp|silk|Xenu|zeal|NING|htdig|lycos|slurp|teoma|voila|yahoo|Sogou|CiBra|Nutch|Java|JNLP|Daumoa|Genieo|ichiro|larbin|pompos|Scrapy|snappy|speedy|vortex|favicon|indexer|Riddler|scooter|scraper|scrubby|WhatWeb|WinHTTP|voyager|archiver|Icarus6j|mogimogi|Netvibes|altavista|charlotte|findlinks|Retreiver|TLSProber|WordPress|wsr-agent|http client|Python-urllib|AppEngine-Google|semanticdiscovery|facebookexternalhit|web/snippet|Google-HTTP-Java-Client-->
<Filters>search|spider|crawl|Bot|Monitor|AlwaysOn</Filters>
</Add>
<Add Type="Microsoft.ApplicationInsights.Web.ClientIpHeaderTelemetryInitializer, Microsoft.AI.Web" />
<Add Type="Microsoft.ApplicationInsights.Web.AzureAppServiceRoleNameFromHostNameHeaderInitializer, Microsoft.AI.Web" />
<Add Type="Microsoft.ApplicationInsights.Web.OperationNameTelemetryInitializer, Microsoft.AI.Web" />
<Add Type="Microsoft.ApplicationInsights.Web.OperationCorrelationTelemetryInitializer, Microsoft.AI.Web" />
<Add Type="Microsoft.ApplicationInsights.Web.UserTelemetryInitializer, Microsoft.AI.Web" />
<Add Type="Microsoft.ApplicationInsights.Web.AuthenticatedUserIdTelemetryInitializer, Microsoft.AI.Web" />
<Add Type="Microsoft.ApplicationInsights.Web.AccountIdTelemetryInitializer, Microsoft.AI.Web" />
<Add Type="Microsoft.ApplicationInsights.Web.SessionTelemetryInitializer, Microsoft.AI.Web" />
</TelemetryInitializers>
<TelemetryModules>
<Add Type="Microsoft.ApplicationInsights.DependencyCollector.DependencyTrackingTelemetryModule, Microsoft.AI.DependencyCollector">
<ExcludeComponentCorrelationHttpHeadersOnDomains>
<!--
Requests to the following hostnames will not be modified by adding correlation headers.
Add entries here to exclude additional hostnames.
NOTE: this configuration will be lost upon NuGet upgrade.
-->
<Add>core.windows.net</Add>
<Add>core.chinacloudapi.cn</Add>
<Add>core.cloudapi.de</Add>
<Add>core.usgovcloudapi.net</Add>
</ExcludeComponentCorrelationHttpHeadersOnDomains>
<IncludeDiagnosticSourceActivities>
<Add>Microsoft.Azure.EventHubs</Add>
<Add>Microsoft.Azure.ServiceBus</Add>
</IncludeDiagnosticSourceActivities>
</Add>
<Add Type="Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.PerformanceCollectorModule, Microsoft.AI.PerfCounterCollector">
<!--
Use the following syntax here to collect additional performance counters:
<Counters>
<Add PerformanceCounter="\Process(??APP_WIN32_PROC??)\Handle Count" ReportAs="Process handle count" />
...
</Counters>
PerformanceCounter must be either \CategoryName(InstanceName)\CounterName or \CategoryName\CounterName
NOTE: performance counters configuration will be lost upon NuGet upgrade.
The following placeholders are supported as InstanceName:
??APP_WIN32_PROC?? - instance name of the application process for Win32 counters.
??APP_W3SVC_PROC?? - instance name of the application IIS worker process for IIS/ASP.NET counters.
??APP_CLR_PROC?? - instance name of the application CLR process for .NET counters.
-->
</Add>
<Add Type="Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.QuickPulse.QuickPulseTelemetryModule, Microsoft.AI.PerfCounterCollector" />
<Add Type="Microsoft.ApplicationInsights.WindowsServer.AppServicesHeartbeatTelemetryModule, Microsoft.AI.WindowsServer" />
<Add Type="Microsoft.ApplicationInsights.WindowsServer.AzureInstanceMetadataTelemetryModule, Microsoft.AI.WindowsServer">
<!--
Remove individual fields collected here by adding them to the ApplicationInsighs.HeartbeatProvider
with the following syntax:
<Add Type="Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing.DiagnosticsTelemetryModule, Microsoft.ApplicationInsights">
<ExcludedHeartbeatProperties>
<Add>osType</Add>
<Add>location</Add>
<Add>name</Add>
<Add>offer</Add>
<Add>platformFaultDomain</Add>
<Add>platformUpdateDomain</Add>
<Add>publisher</Add>
<Add>sku</Add>
<Add>version</Add>
<Add>vmId</Add>
<Add>vmSize</Add>
<Add>subscriptionId</Add>
<Add>resourceGroupName</Add>
<Add>placementGroupId</Add>
<Add>tags</Add>
<Add>vmScaleSetName</Add>
</ExcludedHeartbeatProperties>
</Add>
NOTE: exclusions will be lost upon upgrade.
-->
</Add>
<Add Type="Microsoft.ApplicationInsights.WindowsServer.DeveloperModeWithDebuggerAttachedTelemetryModule, Microsoft.AI.WindowsServer" />
<Add Type="Microsoft.ApplicationInsights.WindowsServer.UnhandledExceptionTelemetryModule, Microsoft.AI.WindowsServer" />
<Add Type="Microsoft.ApplicationInsights.WindowsServer.UnobservedExceptionTelemetryModule, Microsoft.AI.WindowsServer">
<!--</Add>
<Add Type="Microsoft.ApplicationInsights.WindowsServer.FirstChanceExceptionStatisticsTelemetryModule, Microsoft.AI.WindowsServer">-->
</Add>
<Add Type="Microsoft.ApplicationInsights.Web.RequestTrackingTelemetryModule, Microsoft.AI.Web">
<Handlers>
<!--
Add entries here to filter out additional handlers:
NOTE: handler configuration will be lost upon NuGet upgrade.
-->
<Add>Microsoft.VisualStudio.Web.PageInspector.Runtime.Tracing.RequestDataHttpHandler</Add>
<Add>System.Web.StaticFileHandler</Add>
<Add>System.Web.Handlers.AssemblyResourceLoader</Add>
<Add>System.Web.Optimization.BundleHandler</Add>
<Add>System.Web.Script.Services.ScriptHandlerFactory</Add>
<Add>System.Web.Handlers.TraceHandler</Add>
<Add>System.Web.Services.Discovery.DiscoveryRequestHandler</Add>
<Add>System.Web.HttpDebugHandler</Add>
</Handlers>
</Add>
<Add Type="Microsoft.ApplicationInsights.Web.ExceptionTrackingTelemetryModule, Microsoft.AI.Web" />
<Add Type="Microsoft.ApplicationInsights.Web.AspNetDiagnosticTelemetryModule, Microsoft.AI.Web" />
</TelemetryModules>
<ApplicationIdProvider Type="Microsoft.ApplicationInsights.Extensibility.Implementation.ApplicationId.ApplicationInsightsApplicationIdProvider, Microsoft.ApplicationInsights" />
<TelemetrySinks>
<Add Name="default">
<TelemetryProcessors>
<Add Type="Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.QuickPulse.QuickPulseTelemetryProcessor, Microsoft.AI.PerfCounterCollector" />
<Add Type="Microsoft.ApplicationInsights.Extensibility.AutocollectedMetricsExtractor, Microsoft.ApplicationInsights" />
<Add Type="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.AdaptiveSamplingTelemetryProcessor, Microsoft.AI.ServerTelemetryChannel">
<MaxTelemetryItemsPerSecond>5</MaxTelemetryItemsPerSecond>
<ExcludedTypes>Event</ExcludedTypes>
</Add>
<Add Type="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.AdaptiveSamplingTelemetryProcessor, Microsoft.AI.ServerTelemetryChannel">
<MaxTelemetryItemsPerSecond>5</MaxTelemetryItemsPerSecond>
<IncludedTypes>Event</IncludedTypes>
</Add>
</TelemetryProcessors>
<TelemetryChannel Type="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.ServerTelemetryChannel, Microsoft.AI.ServerTelemetryChannel" />
</Add>
</TelemetrySinks>
<!--
Learn more about Application Insights configuration with ApplicationInsights.config here:
http://go.microsoft.com/fwlink/?LinkID=513840
-->
<InstrumentationKey>your-instrumentation-key-here</InstrumentationKey>
</ApplicationInsights>
My web.config contains the following:
<system.web>
<compilation debug="true" targetFramework="4.7.2" />
<httpRuntime targetFramework="4.7.2" />
<httpModules>
<add name="TelemetryCorrelationHttpModule" type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, Microsoft.AspNet.TelemetryCorrelation" />
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
</httpModules>
</system.web>
This documentation suggests that I can set the instrumentation key in code (such as in an AppInitialize method as suggested by this answer), but it doesn't seem to work.
TelemetryConfiguration configuration = TelemetryConfiguration.CreateDefault();
configuration.InstrumentationKey = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
While there appear to be some caveats to this solution (depending on your hosting strategy), it is sometimes possible to add a Global.asax, and use the Application_Start hook to inject your configuration (This answer assumes you're using config transforms).
For example:
using System;
namespace SomeWcfThing
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.Active.InstrumentationKey =
System.Configuration.ConfigurationManager.AppSettings["InstrumentationKey"];
}
}
}
The correct approach is to use TelemetryConfiguration.CreateDefault() method to load any config from disk, then set/change additional values on the generated configuration.
Once the TelemetryConfiguration instance is created pass it to the constructor of TelemetryClient to create the client and start logging.
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>
How to fetch the corresponding value of a key that I would get dynamically. I wish to use the system defined DictionarySectionHandler to do the job, of fetching the data from my custom built config section in the Web.config file
Code block in Web.Config
<section name="domainsource" type="System.Configuration.DictionarySectionHandler"/>
<domainSource>
<add key="0" value="170" />
<add key="1" value="171" />
<add key="2" value="172" />
<add key="3" value="173" />
<add key="12" value="174" />
</domainSource>
Sourcecode in the main cs file from where I wish to retrieve the data from the Web.Config
Hashtable statusCodes = ConfigurationManager.GetSection("domainSource") as Hashtable;
vDomainSource = statusCodes[vDomainID];
This is where I am stuck vDomainID would be a value 0/1/2/3/12, based on this value I need to fetch its respective Source from the Web.Config. Any help on this aspect would be really appreciated.
You have a missspelling in the defintion of the section domainsource -> domainSource. Further ensure that the elemnt is defined in an element. Then it should work.
<configuration>
<configSections>
<section name="domainSource" type="System.Configuration.DictionarySectionHandler"/>
</configSections>
<domainSource>
<add key="0" value="170" />
<add key="1" value="171" />
<add key="2" value="172" />
<add key="3" value="173" />
<add key="12" value="174" />
</domainSource>
</configuration>
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
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.