Code which outputs to WCF trace

Damir Dobric Posts

Next talks:



Follow me on Twitter: #ddobric




The code snippet below has explicitly nothing to do with WCF. But, it can be used to output trace messages to the WCF-log file, which can be read by WCF Trace Tool.
In other words, this example answers two questions:

1. How to trace messages to WCF trace? Messages are mixed with service model messages and/or traces
2. How to trace messages which can be graphically presented by WCF Trace Tool? So, this tool can be used for reading of traces of any kind of .NET application.

After executing the code below follow instructions shown in this post.


public void TestTrace()


TraceSource wcfTrace  = new TraceSource("System.ServiceModel");
Guid currentActivity = Trace.CorrelationManager.ActivityId;
Guid newActivity = Guid.NewGuid();
Guid newActivity2 = Guid.NewGuid();

      Guid newActivity3 = Guid.NewGuid();

wcfTrace.TraceEvent(TraceEventType.Start, 100, "[+] Method entered");

wcfTrace.TraceEvent(TraceEventType.Information, 101, "[+] In current activity");

wcfTrace.TraceEvent(TraceEventType.Start, 100, "[+] Still in current activity");

wcfTrace.TraceEvent(TraceEventType.Information, 102, "[+] Still in current activity");

wcfTrace.TraceTransfer(0, "[+] Start Level 1 activity", newActivity);


.CorrelationManager.ActivityId = newActivity;

wcfTrace.TraceEvent(TraceEventType.Information, 103, "[+] Hello from Level 1");

wcfTrace.TraceEvent(TraceEventType.Warning, 104, "[+] Level 1");
wcfTrace.TraceEvent(TraceEventType.Error, 103, "[+] Level 1");
wcfTrace.TraceEvent(TraceEventType.Critical, 105, "[+] Level 1");
wcfTrace.TraceTransfer(0, "[+] Start Level 2 activity",newActivity2);

Trace.CorrelationManager.ActivityId = newActivity2;

wcfTrace.TraceEvent(TraceEventType.Information, 102, "[+] Hello from Level 2");

wcfTrace.TraceEvent(TraceEventType.Information, 103, "[+] Level 2");

wcfTrace.TraceTransfer(0, "[+] Jump back to level 1", newActivity);

Trace.CorrelationManager.ActivityId = newActivity;

wcfTrace.TraceEvent(TraceEventType.Transfer, 101, "[+] Hello from Level 1");

wcfTrace.TraceEvent(TraceEventType.Information, 102, "[+] Level 1");

wcfTrace.TraceTransfer(0, "[+] Jump back to current",


.CorrelationManager.ActivityId = currentActivity;

wcfTrace.TraceEvent(TraceEventType.Information, 103, "[+] Again in current activity");

wcfTrace.TraceEvent(TraceEventType.Stop, 101, "[+] Method exited");


Posted Mar 27 2009, 10:17 PM by Damir Dobric
Filed under:


Rolf Nebhuth wrote Custom WCF Tracing problem with “propagateActivity” Attribute
on 05-26-2009 10:53

If you want to trace in the same trace source as WCF you can use this Example in Damir Dobric Blog .

DamirDobric wrote Ado.Net Data Services Request Errors
on 09-14-2009 0:37

When working with ADO.NET Data Services you might be suppressed how simple data entities can be exposed is a .Net Community Blog powered by daenet GmbH.