Enable Trace for a WCF service

Posted: January 20, 2017 in WCF

Introduction:

Capturing Logs in an application (server /client side) helps in analyzing and identifying any issues.  Especially it is very helpful when an issue occurs only on Test/Production environments which can’t be duplicated on local developer machines.  

Sometimes, there could be issues at low/framework level.  In those scenarios,  application level logging is also not much helpful as exception might happen even before hitting the initial break point in the application/developer code.  The same is true for a WCF service as well.

In such cases, we could enable WCF service tracing which logs all the steps in WCF request/response pipeline.

Creating a WCF test service and deployment:
Create a simple WCF service and host it on IIS.  Refer this article for more details.

Update web config:

Now we make few changes to web.config. Open the web.config of WCF project and make following changes:

1) Add following diagnostics section in the config file.  Here we are adding listeners and also telling the location to write the log file.  Update the log file path (initializeData) according to folder structure in your machine.

<system.diagnostics>
    <trace autoflush="true" />
    <sources>
        <source name="System.ServiceModel" switchValue="Information, ActivityTracing">
        				<listeners>
          <add name="xml"/>
        </listeners>
      </source>
      <source name="System.ServiceModel.MessageLogging">
        				<listeners>
          <add name="xml"/>
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add name="xml" type="System.Diagnostics.XmlWriterTraceListener" initializeData= "C:\logs\WCFDiagnosticsExample.svclog" />
    </sharedListeners>
</system.diagnostics>

2) Add following configuration inside serviceModel section.

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

Call the WCF service:

Open WCFTestCient and add test service (defined above).  Make a service call using Invoke button.

Once you invoke a method on WCF test service, you should be seeing a log file generated at specified location.   If you observe the size of the file, it may be zero.  That means the log entries are not yet flushed out.  Stop the web application on IIS manager.  Then the log contents will be written to the file.

Service Trace Viewer:

There is a tool called Service Trace Viewer from Microsoft to view the contents of the log file (*.svclog).  To open the tool, open windows explorer and go to following location.   The version folder names may vary depending on what version is installed in your machine.

C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6 Tools
Note: If you don’t see SDK folder, then you need to install Microsoft Windows SDK.

servicetrace

Note: If your wcf service is not hosted by IIS and you have a self host application, then you need to run the app in admin mode to grant permissions to the logging path.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s