This project has moved. For the latest updates, please go here.

Trace

.NET 4.0 Silverlight 4 Silverlight 5 Phone 7.0 Phone 7.1
S.png

Overview

The trace extensions are used to instrument IObservable<T> and IEnumerable<T> sequences with .NET Tracing. Tracing is similar in usage and behavior to the Do extension in Rx, although the trace extensions provide additional semantics and features that are specific for tracing.

Tracing is often used to log the data in a sequence. It can also be used to log error and completion notifications without affecting the behavior of the sequence. Reactive sequences have subscriptions and cancellation behaviors, which can also be logged. There are also trace extensions that accept a string identifying the current observer, or an identifier can be generated automatically.

The trace extensions provide minimalistic methods that write to the System.Diagnostics.Trace Class. They also provide overloads that accept format strings, format functions and a TraceSouce object, for more control over the output.

Using Third-Party Logging, such as Log4Net and NLog

Tracing in Rxx uses the built-in .NET tracing APIs because they provide a simple yet powerful instrumentation model for all applications, which is also consistent with the tracing provided by various APIs in the .NET Framework, such as network tracing, Windows Presentation Foundation (WPF) tracing and Windows Communication Foundation (WCF) tracing.

If your application already depends upon a third-party logging library, such as Log4Net or NLog, then you can write an adapter class that derives from TraceListener to redirect trace output to your preferred logging API. You can also reuse your listener to redirect output from the tracing in .NET APIs for network events, WCF and WPF.

Example

The following C# code snippet was taken from Rxx Labs, which is available on the Downloads tab. The lab defines an observable query using the Rx Timer operator and the Rxx TraceOnNext and TraceSubscriptions operators. Trace output is redirected to the console by an instance of the ConsoleTraceListener class, which is provided by the .NET Framework.

static void Main()
{
	System.Diagnostics.Trace.Listeners.Add(new ConsoleTraceListener());

	var xs = Observable
		.Timer(TimeSpan.Zero, TimeSpan.FromSeconds(1))
		.TraceOnNext(value => "OnNext: " + value)
		.TraceSubscriptions();

	var query = Observable
		.Timer(TimeSpan.Zero, TimeSpan.FromSeconds(3))
		.Select(_ => xs)
		.Switch();

	using (query.Subscribe())
	{
		Console.ReadKey();
	}

	System.Diagnostics.Trace.Listeners.Clear();
}

Platform Support

The entire tracing API is available in the Rxx library for the full .NET Framework 4.0 only.

Silverlight and Windows Phone do not currently include the .NET tracing APIs, thus Rxx's trace extensions are not included on these platforms.

Lab

Download the labs application for .NET 4.0 and then run the Reactive / Tracing and Interactive / Tracing labs. These labs contain working examples of some of the trace extensions in Rxx. The source code for the labs is displayed in the application, for your convenience.

For more information, see Rxx Hands-on Labs.

Resources

Last edited Mar 11, 2012 at 7:30 PM by davedev, version 7