WCF Analytic Tracing
This sample demonstrates how to add your own tracing events into the stream of analytic traces that Windows Communication Foundation (WCF) writes to ETW in .NET Framework 4.6.1. Analytic traces are meant to make it easy to get visibility into your services without paying a high performance penalty. This sample shows how to use the T:System.Diagnostics.Eventing APIs to write events that integrate with WCF services.
For more information about the T:System.Diagnostics.Eventing APIs, see System.Diagnostics.Eventing.
To learn more about event tracing in Windows, see Improve Debugging and Performance Tuning with ETW.
This sample uses the System.Diagnostics.Eventing.EventProvider class, which implements System.IDisposable. When implementing tracing for a WCF service, it is likely that you may use the EventProvider’s resources for the lifetime of the service. For this reason, and for readability, this sample never disposes of the wrapped EventProvider. If for some reason your service has different requirements for tracing and you must dispose of this resource, then you should modify this sample in accordance with the best practices for disposing of unmanaged resources. For more information about disposing unmanaged resources, see Implementing a Dispose Method.
For Web-hosted services, WCF’s analytic traces provide a field, called “HostReference”, which is used to identify the service that is emitting the traces. The extensible user traces can participate in this model and this sample demonstrates best practices for doing so. The format of a Web host reference when the pipe ‘|’ character actually appears in the resulting string can be any one of the following:
If the application is not at the root.
If the application is at the root.
For self-hosted services, WCF’s analytic traces do not populate the “HostReference” field. The
WCFUserEventProvider class in this sample behaves consistently when used by a self-hosted service.
WCF’s ETW Event Provider manifest defines three events that are designed to be emitted by WCF service authors from within service code. The following table shows a breakdown of the three events.
|UserDefinedInformationEventOccurred||Emit this event when something of note happens in your service that is not a problem. For example, you might emit an event after successfully making a call to a database.||301|
|UserDefinedWarningOccurred||Emit this event when a problem occurs that may result in a failure in the future. For example, you may emit a warning event when a call to a database fails but you were able to recover by falling back to a redundant data store.||302|
|UserDefinedErrorOccurred||Emit this event when your service fails to behave as expected. For example, you might emit an event if a call to a database fails and you could not retrieve the data from elsewhere.||303|
Using Visual Studio 2012, open the WCFAnalyticTracingExtensibility.sln solution file.
To build the solution, press CTRL+SHIFT+B.
To run the solution, press CTRL+F5.
In the Web browser, click Calculator.svc. The URI of the WSDL document for the service should appear in the browser. Copy that URI.
Run the WCF test client (WcfTestClient.exe).
The WCF test client (WcfTestClient.exe) is located in the <Visual Studio 2012 Install Dir>\Common7\IDE\ WcfTestClient.exe (default Visual Studio 2012 install dir is C:\Program Files\Microsoft Visual Studio 10.0).
Within the WCF test client, add the service by selecting File, and then Add Service.
Add the endpoint address in the input box.
Click OK to close the dialog.
The ICalculator service is added in the left pane under My Service Projects.
Open the Event Viewer application.
Before invoking the service, start Event Viewer and ensure that the event log is listening for tracking events emitted from the WCF service.
From the Start menu, select Administrative Tools, and then Event Viewer. Enable the Analytic and Debug logs.
In the tree view in Event Viewer, navigate to Event Viewer, Applications and Services Logs, Microsoft, Windows, and then Application Server-Applications. Right-click Application Server-Applications, select View, and then Show Analytic and Debug Logs.
Ensure that the Show Analytic and Debug Logs option is checked. Enable the Analytic log.
In the tree view in Event Viewer, navigate to Event Viewer, Applications and Services Logs, Microsoft, Windows, Application Server-Applications, and then Analytic. Right-click Analytic and select Enable Log.
Test the service using the WCF Test Client.
In the WCF Test Client, double-click Add() under the ICalculator service node.
The Add() method appears in the right pane with two parameters.
Type in 2 for the first parameter and 3 for the second parameter.
Click Invoke to invoke the method.
Go to the Event Viewer window that you have already opened. Navigate to Event Viewer, Applications and Services Logs, Microsoft, Windows, Application Server-Applications.
Right-click the Analytic node and select Refresh.
The events appear in the right pane.
Locate the event with the ID of 303 and double-click it to open it up and inspect its contents.
This event was emitted by the
Add()method of the ICalculator service and has a payload equal to “2+3=5”.
Open Event Viewer.
Navigate to Event Viewer, Applications and Services Logs, Microsoft, Windows, and then Application-Server-Applications. Right-click Analytic and select Disable Log.
Navigate to Event Viewer, Applications and Services Logs, Microsoft, Windows, Application-Server-Applications, and then Analytic. Right-click Analytic and select Clear Log.
Click Clear to clear the events.
There is a known issue in the Event Viewer where it may fail to decode ETW events. You may see an error message that says: “The description for Event ID <id> from source Microsoft-Windows-Application Server-Applications cannot be found. Either the component that raises this event is not installed on your local computer or the installation is corrupted. You can install or repair the component on the local computer.” If you encounter this error, select Refresh from the Actions menu. The event should then decode properly.
The samples may already be installed on your computer. Check for the following (default) directory before continuing.
If this directory does not exist, go to Windows Communication Foundation (WCF) and Windows Workflow Foundation (WF) Samples for .NET Framework 4 to download all Windows Communication Foundation (WCF) and WF samples. This sample is located in the following directory.