Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply an App.config to my DLL assembly?

I'm writing a class library as an abstraction to use for logging in any application, service, etc. that I write. I'm making it decently robust by making it very configurable to suit my needs for most application/service logging scenarios that I come across.

The config is designed to specify things such as:

  • What logging level to write
  • Write to one log file for all levels
  • Write to separate files per level
  • Logging cutoff (periodic, app event, byte size restricted)
  • Log file expiration (delete log files after file age)
  • Write as flat text or XML
  • Log file name format specification
  • Whether to prefix filename with date
  • Parent app's name
  • etc, etc, etc...

I've read some other stackoverflow questions regarding configs for DLL assemblies and it causing conflict between the app.config for the hosting assembly/app. I believe that my assembly has just cause to provide a config file.

Is this a good scenario for that occasion? Is it perhaps a better idea to bake my own config into my project so that my logger reads from XML files to retrieve config values?

like image 257
Jeff LaFay Avatar asked Jul 15 '10 13:07

Jeff LaFay


4 Answers

What you could do is

  • create a custom configuration section (using e.g. the COnfiguration Section Designer tool)

  • put your assembly's configuration into a separate MyAssembly.config file

  • reference that assembly config file from your host app's config:

    <configuration>
       <configSections>
           <section name="YourAssembly" 
                    type="YourAssembly.ConfigSection, YourAssembly" />
       </configSections>
    
       <YourAssembly configSource="MyAssembly.config" />
    </configuration>
    

That way, you can "externalize" your configuration into a separate config file which you have only once (in your assembly's project), and any project needing it just needs those settings in its own config file.

like image 123
marc_s Avatar answered Oct 17 '22 21:10

marc_s


Sounds like a custom config section would work well in your case. Many libraries, such as the Enterprise Library do exactly this. Check out the MSDN article about creating one.

like image 37
wsanville Avatar answered Oct 17 '22 21:10

wsanville


The .NET config mechanism is not meant to handle configuration files for DLLs. You should configure your application with appropriate settings and pass them on to the class you are instantiating from the DLL.


It is possible to add settings to a DLL project as you'd usually do for applications. All you then need to do is copy the relevant sections and entries into the application's app.config manually and it will work.

It is, however, still true that there's no point copying the DLL's config file. It will not be read.

like image 39
Thorsten Dittmar Avatar answered Oct 17 '22 22:10

Thorsten Dittmar


Another mechanism is to have a seperate configuration file (*.dll.config) for your assembly. The technique is shown here: http://blog.rodhowarth.com/2009/07/how-to-use-appconfig-file-in-dll-plugin.html

The above, imitate the standard app.config technique for assemblies.

In my opinion the dll configuration reading code should only exist in the corresponding dll and in a seperate class - with single responsibility of reading configuration entries from the *.dll.config. This is a nice way of having configuration file for an assembly in a way similar to the configuration file (app.config) an executable can have.

like image 31
Anand Patel Avatar answered Oct 17 '22 22:10

Anand Patel