Saturday, May 3, 2014

Trace WCF

Very Often people ask to me how to configure web.config and app.config of .net website and .net application to emit trace information about WCF communication. So every time I search in my projects those settings.
To avoid this search I put in this blog those settings.

app.config
Below there is a portion of app.config that activate two trace and generate two file.
client_messages.svclog contains trace about messages exchanged between client and server.
client_traces.svclog contains trace about detailed information of System.ServiceModel Activities. 

Principal config sections are: <system.diagnostics> and <diagnostics>.
<system.diagnostics> section is child of  <configuration> element and in this example is at the same level of <appSettings>
<diagnostics> section is child of <system.serviceModel> element that is child of   <configuration> which is at the same level of <appSettings> and <system.diagnostics>

  ...
  </appSettings>
  ...
  <system.diagnostics>
    <sources>
      <source 
        name="System.ServiceModel.MessageLogging"           
        switchValue="Verbose">
        <listeners>
          <add 
            name="xml"       
            type="System.Diagnostics.XmlWriterTraceListener"
            initializeData="c:\temp\client_messages.svclog"/>
        </listeners>
      </source>
      <source 
        name="System.ServiceModel" 
        switchValue="Verbose" 
        propagateActivity="true">
        <listeners>
          <add 
            name="traceListener"
            type="System.Diagnostics.XmlWriterTraceListener" 
            initializeData="c:\temp\client_traces.svclog" />
        </listeners>
      </source>
    </sources>
    <trace autoflush="true"/>
  </system.diagnostics>

  <system.serviceModel>
    <diagnostics>
      <messageLogging 
        logEntireMessage="true"    
        logMalformedMessages="true"
        logMessagesAtServiceLevel="true"
        logMessagesAtTransportLevel="true"
        maxMessagesToLog="1000" />
      <endToEndTracing 
        activityTracing="true"
        messageFlowTracing="true" />

    </diagnostics>
  </system.serviceModel>

web.config
Below there is a portion of web.config that activate two trace and generate two file.
server_messages.svclog contains trace about messages exchanged between server and client.
server_traces.svclog contains trace about detailed information of System.ServiceModel Activities. 

Principal config sections are: <system.diagnostics> and <diagnostics>.
<system.diagnostics> section is child of  <configuration> element and in this example is at the same level of <system.web>
<diagnostics> section is child of <system.serviceModel> element that is child of   <configuration> which is at the same level of <system.weband <system.diagnostics>


  ...
  </system.web>
  ... 
  <system.diagnostics>
    <sources>
      <source 
        name="System.ServiceModel.MessageLogging" 
        switchValue="Verbose">
        <listeners>
          <add 
            name="xml" 
            type="System.Diagnostics.XmlWriterTraceListener" 
            initializeData="c:\temp\server_messages.svclog"/>
        </listeners>
      </source>
      <source 
        name="System.ServiceModel" 
        switchValue="Verbose" 
        propagateActivity="true">
        <listeners>
          <add 
            name="traceListener" 
            type="System.Diagnostics.XmlWriterTraceListener"                 initializeData="c:\temp\server_traces.svclog" />
        </listeners>
      </source>
    </sources>
  </system.diagnostics>

  <system.serviceModel>
    <diagnostics>
      <messageLogging 
        logEntireMessage="true" 
        maxMessagesToLog="1000" 
        logMessagesAtServiceLevel="true"        
        logMalformedMessages="true" 
        logMessagesAtTransportLevel="true"/>

    </diagnostics>
  </system.serviceModel>

Note 
Notate that every file has ".svclog" as file name extension.
In this way the file are opened automatically by Microsoft Service Trace Viewer.

No comments:

Post a Comment