How to enable WCF traces programmatically?

wcf

Solution

You first need to access the trace object by name, as its defined in the .config file. For example:

TraceSource ts = new TraceSource("System.ServiceModel");

Then you can set the filter level to all, none or anything in between:

ts.Switch.Level = SourceLevels.Off;   // nothing
ts.Switch.Level = SourceLevels.All;   // everything
ts.Switch.Level = SourceLevels.Warning;   //warning or higher

BTW - the `TraceSource` class is in the `System.Diagnostics` namespace, so don't forget the appropriate `using` statement.

Problem

Is there a way to enable/disable the WCF trace/logging for a perticular end point without changing the web.config ?

Original source