views:

74

answers:

3

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?

+1  A: 

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.

wsanville
+2  A: 

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.

Thorsten Dittmar
Since I'll be using this for a wide array of software applications, wouldn't it be better to config it on the fly with a config file I could hand edit quickly rather than populating properties when instantiating a logger in the host app?
jlafay
@jlafay - no, then you're adding a hidden dependency to your DLL. You can still make a config file - just make your *applications* read the config file (or a portion of that file that you copy into their configs) and pass the settings to the classes from your DLL. (and +1.)
Jeff Sternal
+3  A: 

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.

marc_s
+1 for the link. I've been manually writing ConfigurationSection and ConfigurationSectionGroup classes for years. This tool looks like a lifesaver!
Toby
I think this is the best solution to my config needs. Thanks for the link to the config section designer too!
jlafay