WCF CommunicationException: understanding the root cause - wcf

I am writing an application that connects to the WCF service.
When I receive CommunicationException on the client side I need to understand whether the problem is on the service side or due to the invalid client configuration.

You can add the following section to your WCF host config file:
<system.diagnostics>
<sources>
<source name="System.ServiceModel" switchValue="Information, ActivityTracing" propagateActivity="true">
<listeners>
<add name="xml" type="System.Diagnostics.XmlWriterTraceListener" initializeData="logs\WCFLog.svclog"/>
</listeners>
</source>
</sources>
</system.diagnostics>
this will create an svclog file which can give you more information about the cause of the connection problem.

Related

In WCF or at machine level, where can I found the list of Known Pii

To log known Personally identifiable information in windows one can configure machine.config file as below...
<configuration>
<system.serviceModel>
<machineSettings enableLoggingKnownPii="true" />
</system.serviceModel>
</configuration>
And in WCF configuration file configuration is required as below
<configuration>
<system.diagnostics>
<sources>
<source name="System.ServiceModel.MessageLogging" logKnownPii="true">
<listeners>
...
</listeners>
</source>
<source name="System.ServiceModel" switchValue="Verbose, ActivityTracing">
<listeners>
...
</listeners>
</source>
</sources>
</system.diagnostics>
</configuration>
Now the question is when the logKnownPii=false what all information will not be logged or in other way what all information logs I can see in log files after turning the setting ON (logKnownPii=true).
I got the below blog that explains how to add [PII] attribute to data contract, and that is what I was looking for.
http://blogs.msdn.com/b/martijnh/archive/2008/08/18/suppressing-personal-identifiable-information-pii-in-wcf-log-files.aspx

Trace all the calls (+ their stack) made to a WCF service

I have a WCF Service which is currently in Production. The code performance are not where we would like them to be and we are unable to reproduce in our Staging environment.
I was wondering if it is possible to log every single method call made to the service and by the service. Essentially I would like a sequential list of all the calls and time stamps (our code isn't multi-threaded).
Is there a way to achieve that without having to instrument the binaries. Is there a level of tracing under the system.diagnostic node in the web.config that we could change?
Have you configured tracing in your configuration file? This is a good article on the subject.
Here is a sample configuration you can use and modify for your needs:
<system.diagnostics>
<trace autoflush="true" />
<sources>
<source name="System.ServiceModel"
switchValue="Information, ActivityTracing"
propagateActivity="true">
<listeners>
<add name="ServiceModel"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData="C:\ServiceModel.svclog" />
</listeners>
</source>
<source name="System.ServiceModel.MessageLogging">
<listeners>
<add name="MessageLogging"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData="C:\MessageLogging.svclog" />
</listeners>
</source>
</sources>
</system.diagnostics>
<system.serviceModel>
<diagnostics>
<messageLogging logEntireMessage="True"
logMalformedMessages="False"
logMessagesAtServiceLevel="True"
logMessagesAtTransportLevel="False"
maxMessagesToLog="10000"
maxSizeOfMessageToLog="10000" />
</diagnostics>
</system.serviceModel>
Use the Service Trace Viewer Tool (SvcTraceViewer.exe) to view the resulting logs.
Check WCF Tracing and optionally also WCF message logging and use SvcTraceViewer to check collected data - you can alternatively build your trace listener for logging traces for example to database. WCF also provides performance counters.

wcf message logging uploading files

I'm sending files from clients to a server using WCF with different bindings. I need to know the sizes of the packets that the client send. How should I configure diagnostics in the config?? Or how can I view this?? Thanks.
To enable diagnostics, edit .config file
<system.diagnostics>
<trace autoflush="true" />
<sources>
<source name="System.ServiceModel" switchValue="Information, ActivityTracing" propagateActivity="true">
<listeners>
<add name="sdt" type="System.Diagnostics.XmlWriterTraceListener" initializeData="App_Data\WCF.svclog" />
</listeners>
</source>
</sources>
</system.diagnostics>
Then you can open .svclog file with Microsoft Service Trace Viewer
See also:
Configuring Tracing at MSDN
Administration and Diagnostics at MSDN
Configuring Message Logging at MSDN

How to turn on WCF tracing?

Update:
I have been trying to turn on WCF tracing, but still no success... Below is my lastest update.
Do I need a permission to write to the below location?
<system.diagnostics>
<trace autoflush="true" />
<sources>
<source name="System.ServiceModel"
switchValue="Information, ActivityTracing"
propagateActivity="true">
<listeners>
<add name="sdt"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData= "#\\myservername\folder1\traces.svclog" />
</listeners>
</source>
</sources>
</system.diagnostics>
I am using .NET Framework 3.5.
What is the step-by-step instruction to turn on the WCF tracking for debugging purposes?
The following configuration taken from MSDN can be applied to enable tracing on your WCF service.
<configuration>
<system.diagnostics>
<sources>
<source name="System.ServiceModel"
switchValue="Information, ActivityTracing"
propagateActivity="true" >
<listeners>
<add name="xml"/>
</listeners>
</source>
<source name="System.ServiceModel.MessageLogging">
<listeners>
<add name="xml"/>
</listeners>
</source>
<source name="myUserTraceSource"
switchValue="Information, ActivityTracing">
<listeners>
<add name="xml"/>
</listeners>
</source>
</sources>
<sharedListeners>
<add name="xml"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData="Error.svclog" />
</sharedListeners>
</system.diagnostics>
</configuration>
To view the log file, you can use "C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\SvcTraceViewer.exe".
If "SvcTraceViewer.exe" is not on your system, you can download it from the "Microsoft Windows SDK for Windows 7 and .NET Framework 4" package here:
Windows SDK Download
You don't have to install the entire thing, just the ".NET Development / Tools" part.
When/if it bombs out during installation with a non-sensical error, Petopas' answer to Windows 7 SDK Installation Failure solved my issue.
In your web.config (on the server) add
<system.diagnostics>
<sources>
<source name="System.ServiceModel" switchValue="Information, ActivityTracing" propagateActivity="true">
<listeners>
<add name="traceListener" type="System.Diagnostics.XmlWriterTraceListener" initializeData="C:\logs\Traces.svclog"/>
</listeners>
</source>
</sources>
</system.diagnostics>
Go to your Microsoft SDKs directory. A path like this:
C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6 Tools
Open the WCF Configuration Editor (Microsoft Service Configuration Editor) from that directory:
SvcConfigEditor.exe
(another option to open this tool is by navigating in Visual Studio 2017 to "Tools" > "WCF Service Configuration Editor")
Open your .config file or create a new one using the editor and navigate to Diagnostics.
There you can click the "Enable MessageLogging".
More info: https://msdn.microsoft.com/en-us/library/ms732009(v=vs.110).aspx
With the trace viewer from the same directory you can open the trace log files:
SvcTraceViewer.exe
You can also enable tracing using WMI. More info:
https://msdn.microsoft.com/en-us/library/ms730064(v=vs.110).aspx
Instead of you manual adding the tracing enabling bit into web.config you can also try using the WCF configuration editor which comes with VS SDK to enable tracing
https://stackoverflow.com/a/16715631/2218571

WCF REST Debugging

How can I log what xml is sent to my WCF REST service prior to being deserialized into my datacontract class?
You can use WCF tracing to log the raw XML messages.
Tracing is not enabled by default. You can enable and configure tracing by editing the application’s configuration file. The following .config example enables WCF tracing with raw message logging:
<configuration>
<system.serviceModel>
<diagnostics>
<messageLogging maxMessagesToLog="30000"
logEntireMessage="true"
logMessagesAtServiceLevel="true"
logMalformedMessages="true"
logMessagesAtTransportLevel="true">
</messageLogging>
</diagnostics>
</system.serviceModel>
<system.diagnostics>
<sources>
<source name="System.IdentityModel"
switchValue="Verbose"
logKnownPii="true">
<listeners>
<add name="xml" />
</listeners>
</source>
<!-- Log all messages in the 'Messages' tab of SvcTraceViewer. -->
<source name="System.ServiceModel.MessageLogging">
<listeners>
<add name="xml" />
</listeners>
</source>
<!-- ActivityTracing and propogateActivity are used to
flesh out the 'Activities' tab in SvcTraceViewer to
aid debugging. -->
<source name="System.ServiceModel"
switchValue="Error, ActivityTracing"
propagateActivity="true">
<listeners>
<add name="xml" />
</listeners>
</source>
<!-- This records Microsoft.IdentityModel generated traces,
including exceptions thrown from the framework. -->
<source name="Microsoft.IdentityModel" switchValue="Warning">
<listeners>
<add name="xml" />
</listeners>
</source>
</sources>
<sharedListeners>
<add name="xml"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData="C:\logs\trace.svclog" />
</sharedListeners>
<trace autoflush="true" />
</system.diagnostics>
</configuration>
You can read more about WCF Tracing from MSDN: Configuring Tracing.
Microsoft provides a Service Trace Viewer Tool to read .svclog files.
Make sure the path defined in initializeData is writable by your service.
If you want to look at the raw HTTP traffic, a proxy tool like Fiddler is the simplest way. You'll be able to see all the information that's been POST/PUT'd to your REST service.
If you mean "log" as in "always write the HTTP traffic to a specific location on file," then you can use the built-in tracing to do most of that. Here is a link to an example of doing this, otherwise just look for "WCF tracing" online. You'll find a ton of great examples.