1

I have the following config file:

  <system.diagnostics>
    <trace autoflush="true" indentsize="1" >
      <listeners>
        <add name="dbgTrace" type="System.Diagnostics.TextWriterTraceListener" initializeData="C:\MyLogs\MyApp\Logs\LogFile.log" />
      </listeners>
    </trace>
  </system.diagnostics> 

So I can read the tracelisteners collection like this:

TraceListenerCollection tlc = System.Diagnostics.Trace.Listeners;

and get the TraceListener from it, but the problem is, that I can not access initializeData property. There are only Name, Type, IndentLevel as public properties.

Is there any workaround?

the berserker
  • 1,553
  • 3
  • 22
  • 39

3 Answers3

3

Done using System.Reflection:

FieldInfo fInfo = OurListener.GetType().GetField("initializeData", BindingFlags.NonPublic | BindingFlags.Instance); 
string filePath = (string)fInfo.GetValue(OurListener);
the berserker
  • 1,553
  • 3
  • 22
  • 39
2
var listener = (TextWriterTraceListener)Trace.Listeners["dbgTrace"];
var writer = (StreamWriter)listener.Writer;
var stream = (FileStream)writer.BaseStream;
Console.WriteLine(stream.Name);
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • this will crash if file does not exist in the filesystem. Actually that's the reason I need that info - to create file and folder: This should do it, though the value i get is null :/ System.Reflection.FieldInfo fInfo = OurListener.GetType().GetField("initializeData", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); string filePath = (string)fInfo.GetValue(OurListener); – the berserker Nov 20 '09 at 12:00
1

The constructor overload that has one 'name' parameter is called if the 'initializeData' attribute is specified in the config file, and that one constructor parameter passes the 'initializeData' attribute value. Also the value passed in the 'name' parameter is assigned to the 'Name' property of the TraceListener (instead of the value of the 'name' attribute in the config file).

JoeF
  • 11
  • 1