tags:

views:

19

answers:

2

So I have my WCF service right now running self-hosted on a console app, but I also have them on IIS. The thing is I'm learning to understand the Service Trace Viewer Tool, and of course, now I want to go a little further and write my own messages to include in the trace files.

I tought it should be easy but Trace.TraceInformation writes nothing to the trace file.

What am I missing?

This is what my config looks like:

<configuration>
 <system.diagnostics>
  <sources>
   <source name="System.ServiceModel.MessageLogging" switchValue="Warning, ActivityTracing">
    <listeners>
     <add type="System.Diagnostics.DefaultTraceListener" name="Default">
      <filter type="" />
     </add>
    </listeners>
   </source>
   <source name="System.ServiceModel" switchValue="Verbose,ActivityTracing"
    propagateActivity="true">
    <listeners>
     <add type="System.Diagnostics.DefaultTraceListener" name="Default">
      <filter type="" />
     </add>
     <add name="ServiceModelTraceListener">
      <filter type="" />
     </add>
    </listeners>
   </source>
  </sources>
  <sharedListeners>
   <add initializeData="C:\_sebastian\dev\gomez\WCFTraceTest\Server\App_tracelog.svclog"
    type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
    name="ServiceModelTraceListener" traceOutputOptions="Timestamp">
    <filter type="" />
   </add>
  </sharedListeners>
 </system.diagnostics>
 <system.serviceModel>
  <diagnostics>
   <messageLogging logMalformedMessages="true" logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="true"/>
  </diagnostics>
A: 

As you can see here, you'll need to call Flush on the Trace to flush it to the listeners (or turn on AutoFlush). Are you doing that?

steinar
nop, that's not it... I've tried with same luck.Note that if I debug my service I have only one listener called System.Diagnostics.DefaultTraceListener and ServiceModelListener is not there :( how can i load it?
sebastian
A: 

Add your own source to the config file:

   <source name="MyNameSpace.MySource" switchValue="Verbose,ActivityTracing"
    propagateActivity="true">
    <listeners>
     <add type="System.Diagnostics.DefaultTraceListener" name="Default">
      <filter type="" />
     </add>
     <add name="ServiceModelTraceListener">
      <filter type="" />
     </add>
    </listeners>
   </source>

This is not strictly necessary but if you use the SvcTraceViewer it will be helpful to be able to separate WCF traces from your own traces. Next in any class that you want to do traces from create an instance of TraceSource:

class SomeClass{
  TraceSource _traceSource = new TraceSource("MyNamespace.MySource");

  void SomeMethod(){
    // Anywhere you want to trace you can call the TraceInformation(String) method:
    _traceSource.TraceInformation("My trace message");
  }
}

You can call Flush every time you trace but I would not recommend it. The classes are optimized to disrupt your code flow as little as possible, which means that a trace may not show up in the file immediately, as the actual writing to the file takes place on another thread. You could call Flush during your application shutdown however.

Steve Ellinger