Question

I'm working on shipping a couple of libraries and I'm planning to use TraceSource to implement some diagnostic tracing in them. I also want to allow users to attach/configure their own listeners, switches, filters etc.

Since libraries don't ship with config files, are there any existing patterns that neatly accomplish this?

I've been thinking of the following: Have a logging.config file that ships with the library and show-cases the sources that my library uses but doesn't contain any listeners. Next, in my library, I'll read both from the logging.config and app.config of the user and override the logging.config with any listeners defined in the app.config file. Does this sound like an OK pattern?

Was it helpful?

Solution

The System.Diagnostic section of the app.config (or web.config) is allowed to point to an external file, e.g.

<system.diagnostics configSource="system.diagnostics.config" />

As far as I know, system.diagnostics is set up to allow for just one system.diagnostics section, and not two that get merged. It might be possible with ad hoc logging systems. (I'm presuming this is about the System.Diagnostic's trace library since you have a tag of TraceSource.

So you would ship your library with a few .config files with different listener scenarios (maybe a pre-configed section for Console trace or trace to a file). The user would only need to add the configSource to their app.config to see trace, which is a typical scenario-- temporarily adding trace to deal with a specific problem.

If the consumer of the library is sophisticated enough to want to see the trace of a 3rd party library, they might also be okay to configure their own system.dignostics section as they see fit, you just need to somehow (via documentation) let the user know that you are using System.Diagnostics and what the TraceSource names are.

For example, the .NET framework uses TraceSource in a few namespaces (System.ServiceModel and System.Networking) but unless you run a decompiler you might never realize that such trace exists and can be activated by library users. In otherwords, System.Diagnostic trace in libraries has a discoverability problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top