external configuration file for enterprise library - c#

If i use an external configuration file for Microsoft enterprise library for my program, then all the configuration for enterprise library must reside in the external configuration file? can i still have part of the configuration in the hosting configuration file?

If I understand you correctly you want to configure Entlib in an external file, and override some settings on your app.config?
If that's the case, check the EntLib Hands-On Labs. Particularly take a look at the configuration HOL; it shows you how to inherit configuration from a shared location, and override it.

I am using Enterprise Library 5.0
I needed to reference my enterprise library config outside the app.config.
I used the following with my own logging factory:
var configSource = new FileConfigurationSource("EntiLib.config");
var logWriterFactory = new LogWriterFactory(configSource);
var logWriter = logWriterFactory.Create();
I was able to find this solution on CodePlex
answer on code plex

Good links by Nicolas and Peter. Glad you found the hands-on labs useful. In addition, please take a look at this chapter that we wrote to explain various configuration scenarios, including:
reading configuration information from a wide range of sources.
enforcing common configuration settings across multiple applications.
sharing configuration settings between applications.
specifying a core set of configuration settings that applications can inherit.
merging configuration settings that are stored in a shared location.
creating different configurations for different deployment environments.

Related

Do we really need ApplicationInsights.config file in C#.NET project?

To log a piece of information like errors, information, warnings and many more we used log4NET library and logging in a local file. But our new requirement, Along with file we need to log this information into Azure Application Insights.
We are working with .NET C# Library,
Is really required ApplicationInsights.config file with .NET C# Project? If not what is the best approach to solving this problem.
Is caching applicationInsights.config instrumentKey? if yes then what will be the solution?
Scenario 1: Tried without adding ApplicationInsights.config file
Output: Log4Net logs are not adding in Azure Application Insights
Scenario 2: Tried with adding ApplicationInsights.config file
Output: Log4Net logs are coming
Scenario 3: When I add Nuget API for ApplicationInsights.Log4NetAppender, It creates log4Net section and includes AIAppender. We have separate log4Net.config and it has only File Appender. We want to use only log4Net.config and remove log4net section from App.Config file.
Output: I removed log4Net section from App.Config and added it in log4Net.config and tested but logs are not adding in Application Insights.
Scenario 4: Implemented TelemetryClient through C# code with ApplicationInsights.config logs are coming in Application Insights
TelemetryClient telemetryClient = new TelemetryClient();
telemetryClient.InstrumentKey =<Your Instrument Key>;
// To test
telemetryClient.TrackTrace("Test through code");
The configuration of Application Insights can be done by any combination of config file and code: only the one, or the other, or both of them.
If you find an ApplicationInsights.config file in your project, it was most probably created by installing one of the Application Insights (AI) Nuget packages, but you can remove it and make the same configuration by code.
To allow the mix of configuration by config file and code, the AI clients and configuration classes have GetDefault() or Active static members that give access to an object which can be used globally. For example, you can see that the Log4Net appender TelemetryClient.cs
uses the active telemetry configuration:
public TelemetryClient() : this(TelemetryConfiguration.Active)
If you check the docs for TelemetryConfiguration.Active you'll see this:
Gets the active TelemetryConfiguration instance loaded from the ApplicationInsights.config file. If the configuration file does not exist, the active configuration instance is initialized with minimum defaults needed to send telemetry to Application Insights.
NOTE: Things are a bit different depending on the platform. For example in .NET Core is more advisable to use dependency injection. Have a look under at your case under Code-base monitoring.
For example, if you have a configuration file like this:
<ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings">
<InstrumentationKey>exxxxxxe-axxf-4xx6-bxx2-7xxxxxxxxxx3</InstrumentationKey>
this would take the instrumentation key from it:
_configuration = TelemetryConfiguration.CreateDefault();
If you remove the config file, this will not fail, but you'll need to do set the instrumentation key in code:
_configuration.InstrumentationKey = "exxxxxxe-axxf-4xx6-bxx2-7xxxxxxxxxx3";
If your config file include intializers, modules or whatever, you can also configure them by code. For example:
<TelemetryInitializers>
<Add Type="...MyInitializer, ..." />
</TelemetryInitializers>
can be setup in code like this:
_configuration.TelemetryInitializers.Add(new MyInitializer(...));

How to customize class library config settings across projects & clients

Apologies if this has been asked before; after two days of searching I can only find partial answers that don't fully relate to my situation, and are difficult to follow with my lack of experience.
I have a solution that contains four projects:
Class library (containing database connection strings, email server settings, plus lots of other settings)
Web application (web forms)
Web application (MVC)
Web API
Projects 2,3 & 4 all reference the class library, and use the database connection strings, etc, to function. These projects also contain their own additional settings in web.config, bespoke to that project.
Everything works great so far... However, I now need to publish client-specific versions of my solution, e.g. the solution for ClientABC requires different settings for each project than for ClientXYZ. All other aspects remain the same, it is simply the config settings across the four projects that need to change.
From my research, I hit upon something called SlowCheetah which transforms the config files based on the publish profile. That sounded promising, but then I get this problem, where the class library settings aren't pushed into the other projects. I can see bits of useful info in this question, but don't have the experience to apply it to my problem. I'd rather not duplicate the settings into respective project's config file if possible, as that feels messy.
Can anyone please offer me some help as to what's best here? I don't even know if I'm taking the right approach, but am pretty sure I can't be the first ask this?
but then I get this problem, where the class library settings aren't pushed into the other projects
you have to keep in mind that the configuration file is readed by the SturtUp application, your client. Class Library can't run directly, but inside a WebApp or WinApp or ConsoleApp
So, any settings that you put in your ClassLibrary configuration file must be copied in the configuration file of your WebApp.
Generally, I copy some settings from app.config to web.config but, if you search on internet, you can find a method to automate this operation.
I now need to publish client-specific versions of my solution
You can create many configuration profile and use a web.config transformation:
From ToolBar or Build Menu, select Configurazion Manager...
Create all configuration you need for clients
Now you can see different web.configuration files
Now you can specify different configurazion transformation for your ClientABC, ClientXYZ and publish them with specific configuration
EDIT:
So, you can adopt this solution for your Class Library too, or external config file, and include external file in your web.config: External Config

How I prevent to get latest or check-in a specific file automatically in Team Foundation Server?

I work on a team that works on a project. I change my project web config file to set a specific connection string but when I check-in or get latest version of project it changes to others connection strings. I have same problem in WCF Service references. appconfig and xsd files of service references always corrupted when I check-in or get latest version of program from tfs and I have to delete service references and add it again! How can I get rid of this?
We had the same issue on our project (with connection strings), and found a good solution: http://msdn.microsoft.com/en-us/library/ms254494(v=vs.110).aspx
By adding a connections.config file for each developer with his own connection string, we just needed to say that this file must not be a part of Source Control. Then in the web.config connectionString section, you just refer to the connections.config file.
Just be aware that you need to either transform your web.config or add the connections.config when publishing the site.
I know you can do the same about the appSettings section in the web.config.
How you do it with WCF, I don't know - but it sounds strange to me that your are not using the same WCF refence.
There are many solutions.
The team uses the same configuration (e.g. everyone uses localhost references)
You separate user from application settings (do not apply to all kind of settings nor projects)
Use transforms and solution configuration to map have per-environment setting
Use configSource to move config section in separate files that are not under version control
I do not think there is a perfect solution, but maybe you apply a mix of these. I strongly suggest to apply them in the stated order.

NLog Config files for different environments in Nuget Package

I am using the Nuget Packager extension to create a Nuget Package to include my Logger project and Nlog. I have this working correctly but had a question about how do i use the correct config file for multiple environments (Dev/QA/Prod)?
Do I have multiple config files, one for each environment in the package? but how does the correct one get applied in QA for example, since different environments might have different listeners or targets? Whats the best way to accomplish this, an example to show how to do this would be great since i am new to Nuget and Nlog.Thanks for your help!
Nlog supports different locations of its config file. One of the location is the main configuration file of application. For more information please see Nlog documentation
Then we can use config transformations option. Visual studio supports this by default for web firendly projects (ASP.NET MVC, WCF etc.). If you application is of different type you can use an extension called SlowCheetah(NuGet Link). More information can be found at this page.
Config transformation deped on build configurations in visual studio. In web project you can see that web.config has two transformations: web.Debug.config and web.Release.config.
Hope it helps

How can we write our own configuration in App.config

In one of my c# application, i have to write directories path inside config file as following
Main Folder = Sites
Sub Folders = 400, 500, 600, and so on
For each subfolders = Inst, Prof
Inside Inst and Prof , we have multiple files.
Inside my program, i have to fetch all sites (400, 500 etc) and then i have to write loop for all sites for both folders (Inst and Prof) one by one, once DONE then go for next site and so on
What is best way to write above folder structure in App.config file?
You should check out Jon Rista's three-part series on .NET 2.0 configuration up on CodeProject.
Unraveling the mysteries of .NET 2.0 configuration
Decoding the mysteries of .NET 2.0 configuration
Cracking the mysteries of .NET 2.0 configuration
Highly recommended, well written and extremely helpful! This will give you a thorough understanding of the .NET configuration system.
To design your own custom section, there's also a handy tool (Visual Studio add-in) called Configuration Section Designer that will make it very easy and simple to create your own custom section and have it build up all the necessary code to handle that custom section.
There is an easy way to create custom configure . custom-configuration-sections-in-3-easy-steps

Categories

Resources