none
Trace.Write in WCF service to log custom messages RRS feed

  • Question

  • I am trying to use Trace.Write to log custom messages from within a WCF service. I have enabled Tracing and can see the default messages when service gets invoked however, I am not sure how I can view the messages that I logged using Trace.Write from within WCF service. Any pointers?

    I am using SVCTraceViewer utility to view trace logs.

      <system.diagnostics>
        <sources>      
          <source name="System.ServiceModel.MessageLogging" switchValue="Information,ActivityTracing">
            <listeners>
              <add type="System.Diagnostics.DefaultTraceListener" name="Default">
                <filter type="" />
              </add>
              <add name="ServiceModelMessageLoggingListener">
                <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:\WCFLogs\web_messages.svclog" type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            name="ServiceModelMessageLoggingListener" traceOutputOptions="Timestamp">
            <filter type="" />
          </add>
          <add initializeData="C:\WCFLogs\web_tracelog.svclog"
            type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            name="ServiceModelTraceListener" traceOutputOptions="Timestamp">
            <filter type="" />
          </add>
        </sharedListeners>
        <trace autoflush="true" />
      </system.diagnostics>

    Here is Diagnotics from System.ServiceModel

    <diagnostics>
          <messageLogging logEntireMessage="true" logMalformedMessages="true"
            logMessagesAtServiceLevel="false" logMessagesAtTransportLevel="true"
            maxMessagesToLog="3000" maxSizeOfMessageToLog="4000" />
        </diagnostics>

    Tuesday, November 19, 2013 10:41 PM

Answers

  • Hi,

    I see that you want to use the trace.write to log custom message in wcf, for example you have done the following:

    Trace.WriteLine("Error in Line20")
    // You must close or flush the trace to empty the output buffer.
    Trace.Flush();

    Then you need a listener in the Listeners Collection to get the same messages from the trace output methods:

    <configuration>
      <system.diagnostics>
        <trace autoflush="false" indentsize="4">
          <listeners>
            <add name="myListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="TextWriterOutput.log" />
            <remove name="Default" />
          </listeners>
        </trace>
      </system.diagnostics>
    </configuration>
    

    Or you just can create and use a trace listener in code:

    Trace.Listeners.Add(new TextWriterTraceListener("TextWriterOutput.log", "myListener")); 
    Trace.Write("Test message."); 
    // You must close or flush the trace to empty the output buffer. 
    Trace.Flush();

    Then you can view the information in the TextWriterOutput.log.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by diffident Wednesday, November 20, 2013 8:43 PM
    Wednesday, November 20, 2013 7:16 AM
    Moderator