Add a default TraceListener for all TraceSources in App.config
Asked Answered
S

2

7

How can I define a default TraceListener, that is automatically added to all TraceSources, in a net 4.0 c# project?

Currently I have to list every named TraceSource I use in the App.config file like this:

  <system.diagnostics>
  <sharedListeners>
      <add name="MyListener" type="MyListenerType,MyAssemblyName" />
  </sharedListeners>
    <sources>
      <source name="Class1" switchValue="All">
        <listeners><add name="MyListener"></add></listeners>
      </source>
      <source name="Class2" switchValue="All">
        <listeners><add name="MyListener"></add></listeners>
      </source>
      <source name="Class3" switchValue="All">
        <listeners><add name="MyListener"></add></listeners>
      </source>
      ... repeat for a gazillion classes ...
    </sources>
  <system.diagnostics>

I am using a SharedListener that should receive all outputs from all TraceSources, unless otherwise specified. With the above syntax, this requires a manual entry for each TraceSource.

Whenever I introduce a new class with a new TraceSource, I have to update the App.Config. If multiple programs use that assembly, I have to update multiple App.Config. A spelling mistake while updating these entries will not produce any error, it will just silently omit all trace output from the correct source.

Is there a way I can set a default TraceListener via App.config, so that I only have to name specific TraceSources if I want to deviate from the default?

Speculate answered 1/7, 2014 at 11:51 Comment(0)
G
4

I didn't find a great solution to this, so what I did was at least centralize the creation of TraceSources. Then I can add any of the 'trace' listeners in app.config to these newly created sources:

TraceSource toReturn = new TraceSource(name, filterLevel);

//remove the default trace listener; don't 'clear' the listeners entirely, because that would undo changes made in app.config; this is a decent compromise
toReturn.Listeners.Remove("Default");

//add all global trace listeners from the app.config
toReturn.Listeners.AddRange(Trace.Listeners);

return toReturn;

Now any listeners I add to <system.diagnostics> \ <trace> \ <listeners> will be added to all trace sources I create with this code.

Gabie answered 24/9, 2015 at 21:55 Comment(0)
C
1

You could add a default listener in the machine config, but that would affect more apps than you want to affect.

Chloroprene answered 1/7, 2014 at 12:52 Comment(2)
I do have shared Listeners. The Listener MyListener in my example is a shared listener, otherwise I could not refer to it by name when adding it. (I'll try to make this more clear in the question). But with the current configuration, I still need to add this shared listener to every single TraceSource. I am looking for a way to avoid repeating every TraceSource name in the App.Config, while risking missing or misspelling one.Speculate
Sorry. Then you need to either modify machine.config, or do it in code on application start, but that requires reflection to get at the private list of trace sources to add listeners to them all.Chloroprene

© 2022 - 2024 — McMap. All rights reserved.