How to attach a listener in source code?

Jun 30, 2008 at 7:12 PM
I was wondering if there is also support to add a listener without a configuration file and only given it's "name"?

I would expect that there would be a function like Trace.GetTraceSource("another-assembly-trace-source"). Given this tracesource it would be possible to attach at runtime a new listener.

I know that it is possible to update the .config file and call Trace.Refresh, but am looking for a solution for an application without a .config file.

Jul 2, 2008 at 8:39 AM

Yes this is possible. To retrieve an existing configured trace source you just construct one with teh appropriate name.


TraceSource source = new TraceSource("another-assembly-trace-source");
source.Listeners.Add(new MyListener());

If you have a look at our unit tests this technique is used a lot.