Debugging Rx with Seq

Debugging Reactive Extensions can sometimes be a bit tricky. When you have a few stream to watch, pinpointing failures is time consuming, and a can easily become a burden.

Catching first-chance exceptions can be extremely helpful when debugging Rx. However, it's much more useful to see how events progress over time. After coming across a thread about tracing in Reactive Extensions I thought I could make a few improvements to their tracing methods by making use of Seq and Serilog.

Seq is fantastic tool that lets you visualise and explore log data quickly and easily. You can get Seq up and running within minutes. Just download the NuGet for Serilog and install the latest version of Seq.

Rx events in Seq Some events in Seq.

To hook this up in your application grab this updated Trace extension method using Serilog.

public static IObservable<TSource> Trace<TSource>(this IObservable<TSource> source, string name)
{
	var id = 0;
	return Observable.Create<TSource>(observer => {

		var itemId = ++id;
		Action<string, object> trace = (m, v) => Log.Information("{name}{id}: {method}({value})", name, itemId, m, v);

		trace("Subscribe", null);
		IDisposable disposable = source.Subscribe(
			v => { trace("OnNext", v); observer.OnNext(v); },
			e => { trace("OnError", e); observer.OnError(e); },
			() => { trace("OnCompleted", null); observer.OnCompleted(); });

		return () => { trace("Dispose", null); disposable.Dispose(); };
	});
}

Next wire up the trace.

Observable
	.Interval(100).Trace("Interval")
    .Take(2).Trace("Take")
    .Count();

Then just register Seq with Serilog and log away.

Log.Logger = new LoggerConfiguration()
	.WriteTo.Seq("http://localhost:5341")
	.CreateLogger();
Daniel LittleWritten by Daniel Little