共用方式為


WCF Tracing

WCF Tracing can be configured at three different levels.

1. WCF Verbose Traces: Recommended for DEV issue (captures activities and messages).

 INCLUDE THIS SECTION INSIDE CONFIGURATION FILE OF WCF SERVICE / CLIENT APPLICATION. 
IF ALREADY A SIMILAR SECTION IS AVAILABLE, THEN DELETE THE EXISTING SECTION FIRST.
SET VALUE OF 'initializeData' UNDER '<sharedListeners>' TO POINT AT AN ABSOLUTE FOLDER PATH. EXAMPLE : C:\TEMP\app_messages.svclog
===================================================================================================================================

    <system.diagnostics>
        <sources>
            <source name="System.ServiceModel.MessageLogging" switchValue="Verbose,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="web_messages.svclog"
                type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                name="ServiceModelMessageLoggingListener" traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, ProcessId, ThreadId, Callstack">
                <filter type="" />
            </add>
            <add initializeData="web_tracelog.svclog"
                type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                name="ServiceModelTraceListener" traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, ProcessId, ThreadId, Callstack">
                <filter type="" />
            </add>
        </sharedListeners>
    </system.diagnostics>

THIS PART WILL GO INSIDE <system.servicemodel> SECTION
=============================================================================================
    <diagnostics>
      <messageLogging logEntireMessage="true" logMalformedMessages="true"
        logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="true"
        maxSizeOfMessageToLog="52428800" />
    </diagnostics>

EXAMPLE :
=========
<system.serviceModel>
        <diagnostics>
            <messageLogging logEntireMessage="true" logMalformedMessages="true"
                logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="true"
                maxSizeOfMessageToLog="26214445" />
        </diagnostics>
        <bindings>
    .
    .

2. WCF Activity Traces: Recommended for Performance issues (captures only activities).

 INCLUDE THIS SECTION INSIDE CONFIGURATION FILE OF WCF SERVICE / CLIENT APPLICATION. 
IF ALREADY A SIMILAR SECTION IS AVAILABLE, THEN DELETE THE EXISTING SECTION FIRST.
SET VALUE OF 'initializeData' UNDER '<sharedListeners>' TO POINT AT AN ABSOLUTE FOLDER PATH. EXAMPLE : C:\TEMP\web_tracelog.svclog
===================================================================================================================================

  <system.diagnostics>
    <sources>
      <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="web_tracelog.svclog"
        type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
        name="ServiceModelTraceListener" traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, ProcessId, ThreadId, Callstack">
        <filter type="" />
      </add>
    </sharedListeners>
  </system.diagnostics>

3. WCF Warning level traces: Recommended for Production server (captures only warning/exceptions).

 https://docs.microsoft.com/en-us/dotnet/framework/wcf/diagnostics/tracing/recommended-settings-for-tracing-and-message-logging

Special: WCF+System.Net Traces

 <?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel" switchValue="Verbose, ActivityTracing" propagateActivity="true" >
        <listeners>
          <add name="xml"/>
        </listeners>
      </source>
      <source name="System.ServiceModel.MessageLogging">
        <listeners>
          <add name="xml"/>
        </listeners>
      </source>
      <source name="System.Net">
        <listeners>
          <add name="System.Net"/>
        </listeners>
      </source>
      <source name="System.Net.HttpListener">
        <listeners>
          <add name="System.Net"/>
        </listeners>
      </source>
      <source name="System.Net.Sockets">
        <listeners>
          <add name="System.Net"/>
        </listeners>
      </source>
      <source name="System.Net.Cache">
        <listeners>
          <add name="System.Net"/>
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add name="xml"
               type="System.Diagnostics.XmlWriterTraceListener"
                     initializeData="C:\temp\WCFTraces.svclog" />
      <add name="System.Net" type="System.Diagnostics.TextWriterTraceListener"
      initializeData="c:\temp\SNtrace.log" traceOutputOptions = "DateTime" />
    </sharedListeners>

    <switches>
      <add name="System.Net" value="Verbose" />
      <add name="System.Net.Sockets" value="Verbose" />
      <add name="System.Net.Cache" value="Verbose" />
      <add name="System.Net.HttpListener" value="Verbose" />
    </switches>

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

 

I hope this helps!

Thanks
Saurabh Somani