Extending Tracing
This sample demonstrates how to extend the Windows Communication Foundation (WCF) tracing feature by writing user-defined activity traces in client and service code. This allows the user to create trace activities and group traces into logical units of work. It is also possible to correlate activities through transfers (within the same endpoint) and propagation (across endpoints). In this sample, tracing is enabled for both the client and the service. For more information about how to enable tracing in client and service configuration files, see Tracing and Message Logging.
This sample is based on the Getting Started Sample.
Note
The set-up procedure and build instructions for this sample are located at the end of this topic.
Note: |
---|
The samples may already be installed on your computer. Check for the following (default) directory before continuing.
<InstallDrive>:\WF_WCF_Samples
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.
<InstallDrive>:\WF_WCF_Samples\WCF\Basic\Management\ExtendingTracing
|
Tracing and Activity Propagation
User-defined activity tracing allows the user to create their own trace activities to group traces into logical units of work, correlate activities through transfers and propagation, and lessen the performance cost of WCF tracing (for example, the disk space cost of a log file).
Adding Custom Sources
User-defined traces can be added to both client and service code. Adding trace sources to the client or service configuration files allow for these custom traces to be recorded and displayed in the Service Trace Viewer Tool (SvcTraceViewer.exe). The following code shows how to add a user-defined trace source named ServerCalculatorTraceSource
to the configuration file.
<system.diagnostics>
<sources>
<source name="System.ServiceModel" switchValue="Warning" propagateActivity="true">
<listeners>
<add type="System.Diagnostics.DefaultTraceListener" name="Default">
<filter type="" />
</add>
<add name="xml">
<filter type="" />
</add>
</listeners>
</source>
<source name="ServerCalculatorTraceSource" switchValue="Information,ActivityTracing">
<listeners>
<add type="System.Diagnostics.DefaultTraceListener" name="Default">
<filter type="" />
</add>
<add name="xml">
<filter type="" />
</add>
</listeners>
</source>
</sources>
<sharedListeners>
<add initializeData="C:\logs\ServerTraces.svclog" type="System.Diagnostics.XmlWriterTraceListener"
name="xml" traceOutputOptions="Callstack">
<filter type="" />
</add>
</sharedListeners>
<trace autoflush="true" />
</system.diagnostics>....
....
Correlating Activities
To correlate activities directly across endpoints, the propagateActivity
attribute must be set to true
in the System.ServiceModel
trace source. Also, to propagate traces without going through WCF activities, ServiceModel Activity Tracing must be turned off. This can be seen in the following code example.
Note
Turning off ServiceModel Activity Tracing is not the same as having the trace level, denoted by the switchValue
property, set to off.
<system.diagnostics>
<sources>
<source name="System.ServiceModel" switchValue="Warning" propagateActivity="true">
...
</source>
</sources>
</system.diagnostics>
Lessening Performance Cost
Setting ActivityTracing
to off in the System.ServiceModel
trace source generates a trace file that contains only user-defined activity traces, without any of the ServiceModel activity traces included. This results in a log file of much smaller size. However, the opportunity to correlate WCF processing traces is lost.
To set up, build, and run the sample
Ensure that you have performed the One-Time Setup Procedure for the Windows Communication Foundation Samples.
To build the C# or Visual Basic .NET edition of the solution, follow the instructions in Building the Windows Communication Foundation Samples.
To run the sample in a single- or cross-computer configuration, follow the instructions in Running the Windows Communication Foundation Samples.