Trace listener, how to output to different files based on event type c#

1.2k Views Asked by At

I would like to create two listeners, one for Data and one for errors. Each one outputs to different file. I would like to to decide which listener listens in my program

so what i would like:

  <system.diagnostics>
  <trace autoflush="true" indentsize="4">
    <listeners>
      <clear/>
      <add name="AppListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="Output/logs.log"/>
      <add name="ErrorListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="errors.log"/>
    </listeners>
  </trace>

In the program i would then like to make a call as such:

Trace.Writeline("AppListener", "Info: this is information");

Is this possible or something similar ?

1

There are 1 best solutions below

0
On

Not really. The entries in the <listeners> list are all invoked and you don't have control over that from the tracing side. You can create an instance of System.Diagnostics.TextWriterTraceListener yourself in code, and use that directly instead. Depending on what you are trying to achieve exactly, you might find it useful to apply specific filters to the listeners in the <listeners> collection, which would allow them to act on different traces.