WCF traces are very helpful in diagnosing and debugging problems with a deployed WCF service. This post describes how traces generated by a WCF RIA service can be accessed and inspected directly from a web browser in a variety of formats.
WCF traces capture information about the lifetime of a WCF service as well as processing stages for every message. Traces provide much richer information about the inner workings of a WCF service compared to any diagnostic information that may be sent back to the client in the form of SOAP faults or transport specific error codes (e.g. HTTP status codes). In particular, issues related to message processing within the WCF channel stack rarely result in useful diagnostic information propagated back to the client; at the same time, WCF traces usually provide adequate information to diagnose the problem.
WCF traces are collected and captured using the System.Diagnostics infrastructure. In a typical use case one would configure the system to collect the WCF traces in a file on the server. This approach is oftentimes suboptimal for WCF services deployed in shared hosting environments, since access to the file system may be restricted.
Accessing WCF RIA service traces from a web browser
One approach to provide easy access to WCF RIA service traces for debugging and diagnostic purposes is to collect the trace information in memory instead of the file system and expose this information directly over a new service endpoint. WCF RIA Services Toolkit April 2010 supplies a custom implementation of an endpoint factory for WCF RIA services which exposes WCF traces collected within an application domain using a WCF REST endpoint in three formats: ATOM, XML, and HTML.
The feature can be enabled by referencing Microsoft.ServiceModel.DomainServices.Hosting.dll from the WCF RIA Services Toolkit in the web application (make sure to mark the library reference “copy local”) and adding the highlighted sections to the the web.config file (other pre-existing sections omitted for brevity):
1 <configuration> 2 <system.diagnostics> 3 <sources> 4 <source name="System.ServiceModel" switchValue="Information" propagateActivity="false"> 5 <listeners> 6 <add name="InMemoryTraceListener" /> 7 </listeners> 8 </source> 9 </sources> 10 <sharedListeners> 11 <add 12 type="Microsoft.ServiceModel.DomainServices.Hosting.InMemoryTraceListener, Microsoft.ServiceModel.DomainServices.Hosting" 13 name="InMemoryTraceListener" /> 14 </sharedListeners> 15 </system.diagnostics> 16 17 <system.serviceModel> 18 <domainServices> 19 <endpoints> 20 <add 21 name="traces" 22 type="Microsoft.ServiceModel.DomainServices.Hosting.TracingDomainServiceEndpointFactory, Microsoft.ServiceModel.DomainServices.Hosting" 23 maxEntries="200"/> 24 </endpoints> 25 </domainServices> 26 </system.serviceModel> 27 </configuration>
After the service is deployed, traces for all WCF services running in the application domain will be collected in memory and can be accessed from the web browser by navigating to a URL formed by appending “/traces” to the URL of any of the WCF RIA services running in the web application. For example, if the web application containing a WCF RIA service created using the Business Application project template is deployed in the “abc” virtual directory on localhost, and the project namespace is “MyCompany.MyProject”, then WCF traces can be accessed by navigating to:
(The “MyCompany-MyProject-AuthenticationService.svc” can be replaced with a reference to any of the WCF RIA service endpoints in the web application).
If no URL parameters are specified, traces will be returned in the ATOM format.
Note that Internet Explorer version 7 and greater provides a very usable interface for viewing, sorting, and searching through an ATOM feed. By entering “error” in the provided search box one can very quickly locate exception traces.
Other formats can be explicitly requested by specifying a URL parameter, for example:
1 http://localhost/abc/MyCompany-MyProject-AuthenticationService.svc/traces?format=atom 2 http://localhost/abc/MyCompany-MyProject-AuthenticationService.svc/traces?format=xml 3 http://localhost/abc/MyCompany-MyProject-AuthenticationService.svc/traces?format=html
Given that traces are collected in memory, a quota is provided to limit the maximum number of most recent traces that will be kept. By default 200 most recent traces are maintained, but the value can be modified using the “maxEntries” attribute in the configuration file:
1 <domainServices> 2 <endpoints> 3 <add 4 name="traces" 5 type="Microsoft.ServiceModel.DomainServices.Hosting.TracingDomainServiceEndpointFactory, Microsoft.ServiceModel.DomainServices.Hosting" 6 maxEntries="200"/> 7 </endpoints> 8 </domainServices>
Tracing information should only be exposed for access by web clients in controlled development or staging environments for the same reasons one should not propagate all ASP.NET exceptions back to the client in a production deployment.