I recently read Chris Love’s advice on using WCF Tracing to help with troubleshooting.
He turns on the tracing by adding new sections of XML to the app.config file and I have since seen similar recommendations here for the same technique.
However we don’t really want to ship multiple app.config files.
And we definitely don’t want our customers modifying them on production systems!
Is there a way that the various settings for WCF Tracing can be set up in the app.config, but the tracing is turned on/off from code?
Ideally I’d like my application to check the registry and only activate the tracing when a certain value was present.
My suggestion is to use a custom
TraceFilterwhich you apply to all listeners attached to the WCF TraceSources (i.e., “System.ServiceModel”, “System.ServiceModel.MessageLogging”). Inside the TraceFilter’sShouldTrace()method you can then conditionally suppress tracing based on any information that’s available to the app.Here is some sample code which you could use as a starting point. There is a static helper class which determines globally and once during the lifetime of an app whether tracing should be enabled or disabled:
In the App.config you’d configure the TraceFilter like this:
**
Please note that a similar behavior could be achieved by writing a custom Trace Switch. The main difference would be that a TraceSwitch is applied to a TraceSource, and a TraceFilter to a TraceListener. It would be slightly more involved, though.