views:

171

answers:

2

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?

+1  A: 
var listener = (TextWriterTraceListener)Trace.Listeners["dbgTrace"];
var writer = (StreamWriter)listener.Writer;
var stream = (FileStream)writer.BaseStream;
Console.WriteLine(stream.Name);
Darin Dimitrov
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
A: 
System.Reflection.FieldInfo fInfo = OurListener.GetType().GetField("initializeData", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); string filePath = (string)fInfo.GetValue(OurListener);
the berserker