如何查看我的反应式扩展查询在做什么? [英] How can I see what my reactive extensions query is doing?
问题描述
我正在编写一个复杂的 Reactive Extensions 查询,其中包含许多运算符.我怎样才能看到发生了什么?
I'm writing a complex Reactive Extensions query with lots of operators. How can I see what's going on?
我正在询问并回答这个问题,因为它出现了一些并且可能具有很好的一般用途.
I'm asking and answering this as it comes up a fair bit and is probably of good general use.
推荐答案
您可以在开发 Rx 操作符时随意将此函数附加到您的 Rx 操作符以查看发生了什么:
You can append this function liberally to your Rx operators while you are developing them to see what's happening:
public static IObservable<T> Spy<T>(this IObservable<T> source, string opName = null)
{
opName = opName ?? "IObservable";
Console.WriteLine("{0}: Observable obtained on Thread: {1}",
opName,
Thread.CurrentThread.ManagedThreadId);
return Observable.Create<T>(obs =>
{
Console.WriteLine("{0}: Subscribed to on Thread: {1}",
opName,
Thread.CurrentThread.ManagedThreadId);
try
{
var subscription = source
.Do(x => Console.WriteLine("{0}: OnNext({1}) on Thread: {2}",
opName,
x,
Thread.CurrentThread.ManagedThreadId),
ex => Console.WriteLine("{0}: OnError({1}) on Thread: {2}",
opName,
ex,
Thread.CurrentThread.ManagedThreadId),
() => Console.WriteLine("{0}: OnCompleted() on Thread: {1}",
opName,
Thread.CurrentThread.ManagedThreadId)
)
.Subscribe(obs);
return new CompositeDisposable(
subscription,
Disposable.Create(() => Console.WriteLine(
"{0}: Cleaned up on Thread: {1}",
opName,
Thread.CurrentThread.ManagedThreadId)));
}
finally
{
Console.WriteLine("{0}: Subscription completed.", opName);
}
});
}
这是一个示例用法,显示了Range
的细微行为差异:
Here's an example usage, shows a subtle behaviour difference of Range
:
Observable.Range(0, 1).Spy("Range").Subscribe();
给出输出:
Range: Observable obtained on Thread: 7
Range: Subscribed to on Thread: 7
Range: Subscription completed.
Range: OnNext(0) on Thread: 7
Range: OnCompleted() on Thread: 7
Range: Cleaned up on Thread: 7
但是这个:
Observable.Range(0, 1, Scheduler.Immediate).Spy("Range").Subscribe();
给出输出:
Range: Observable obtained on Thread: 7
Range: Subscribed to on Thread: 7
Range: OnNext(0) on Thread: 7
Range: OnCompleted() on Thread: 7
Range: Subscription completed.
Range: Cleaned up on Thread: 7
找出不同之处?
显然,您可以更改此设置以写入日志或调试,或使用预处理器指令对发布版本等进行精益传递订阅...
Obviously you can alter this to write to logs or to Debug, or use preprocessor directives to do a lean pass-through subscription on a Release build etc...
您可以在整个运算符链中应用 Spy
.例如:
You can apply Spy
throughout a chain of operators. e.g.:
Observable.Range(0,3).Spy("Range")
.Scan((acc, i) => acc + i).Spy("Scan").Subscribe();
给出输出:
Range: Observable obtained on Thread: 7
Scan: Observable obtained on Thread: 7
Scan: Subscribed to on Thread: 7
Range: Subscribed to on Thread: 7
Range: Subscription completed.
Scan: Subscription completed.
Range: OnNext(1) on Thread: 7
Scan: OnNext(1) on Thread: 7
Range: OnNext(2) on Thread: 7
Scan: OnNext(3) on Thread: 7
Range: OnCompleted() on Thread: 7
Scan: OnCompleted() on Thread: 7
Range: Cleaned up on Thread: 7
Scan: Cleaned up on Thread: 7
我相信您可以找到丰富的方法以满足您的目的.
I'm sure you can find ways of enriching this to suit your purposes.
这篇关于如何查看我的反应式扩展查询在做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!