Daniel Little Coding Blog
Daniel Little Coding Blog

A blog about .net development and the web.

Daniel Little
Author

Daniel Little (lavinski) is a Brisbane based software developer.

Share


Our Newsletter


Like this blog? Get each new post as soon it's posted.

Tags


Twitter


Debugging Rx with Seq

Daniel LittleDaniel Little

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 Little
Author

Daniel Little

Daniel Little (lavinski) is a Brisbane based software developer.

Comments