RX.Net:使用重试,但记录任何异常 [英] RX.Net : Use Retry but log any Exception

查看:132
本文介绍了RX.Net:使用重试,但记录任何异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是RX的新手,一直在研究错误处理和Retry的使用;我有以下内容(是的,我知道这不是一个真正的"单元测试,但是它给了我一些摆弄的地方!),想知道如何继续重试,但能够记录任何异常?

I am new to RX and have been investigating error handling and the use of Retry; I have the following (yes I know it's not a 'real' unit test but it gives me place to fiddle!!) and was wondering how I go about keeping the Retry but be able to log any Exception?

    [Test]
    public void Test()
    {
        var scheduler = new TestScheduler();

        var source = scheduler.CreateHotObservable(
            new Recorded<Notification<long>>(10000000, Notification.CreateOnNext(0L)),
            new Recorded<Notification<long>>(20000000, Notification.CreateOnNext(1L)),
            new Recorded<Notification<long>>(30000000, Notification.CreateOnNext(2L)),
            new Recorded<Notification<long>>(30000001, Notification.CreateOnError<long>(new Exception("Fail"))),
            new Recorded<Notification<long>>(40000000, Notification.CreateOnNext(3L)),
            new Recorded<Notification<long>>(40000000, Notification.CreateOnCompleted<long>())
        );

        source.Retry().Subscribe(
            l => Console.WriteLine($"OnNext {l}"), 
            exception => Console.WriteLine(exception.ToString()), // Would be logging this in production
            () => Console.WriteLine("OnCompleted"));

       scheduler.Start(
            () => source,
            0,
            TimeSpan.FromSeconds(1).Ticks,
            TimeSpan.FromSeconds(5).Ticks);
    }

这将导致...

OnNext 0
OnNext 1
OnNext 2
OnNext 3
OnCompleted

...除了我想记录发生在2到3之间的异常之外,这正是我想要发生的事情.

...which is exactly what I want to happen apart from fact I would like to log the Exception which occurs between 2 and 3.

是否有一种方法可以使订阅服务器在OnError中看到异常(并记录该异常),然后重新订阅,使其看到3?

Is there a way to allow the Subscriber to see the Exception in OnError (and log it) and then re-subscribe so it sees 3?

谢谢!

推荐答案

您可以这样实现:

source
    .Do(_ => { }, exception => Console.WriteLine(exception.ToString()), () => {})
    .Retry()
    .Subscribe(
        l => Console.WriteLine($"OnNext {l}"),
        //      exception => Console.WriteLine(exception.ToString()), // Would be logging this in production
        () => Console.WriteLine("OnCompleted")
    );

只是为了澄清这里发生的事情:OnError是终止信号.如果错误到达订阅,则将终止流的其余部分. .Retry终止订阅,吞下OnError,然后重新订阅,将两个订阅融合在一起.例如看这个:

Just to clarify what's going on here: OnError is a terminating signal. If the error reached the subscription, that would terminate the rest of the stream. .Retry terminates the subscription, swallows the OnError, and then re-subscribes, melding the two subscriptions together. For example look at this:

source
    .StartWith(-1)
    .Retry()
    .Subscribe(
        l => Console.WriteLine($"OnNext {l}"),
        () => Console.WriteLine("OnCompleted")
    );

您的输出应为

OnNext -1
OnNext 0
OnNext 1
OnNext 2
OnNext -1
OnNext 3
OnCompleted

OnNext -1显示两次,因为每次您订阅时都会显示(RetryOnError之后执行.

The OnNext -1 shows up twice, because it shows up whenever you subscribe (which Retry does after the OnError.

坦率地说,您的测试是一个不好的测试.它破坏了"Rx合同",即通知遵循以下模式:

Your test observable is frankly a bad test. It breaks the "Rx Contract" which is that notifications follow the following pattern:

OnNext* (OnCompleted | OnError)? 

即0个或更多OnNext通知,后跟一个可选的OnError或可选的OnCompleted. OnErrorOnCompleted都不能跟随任何类型的通知.

That is, 0 or more OnNext notifications, followed by an optional OnError or an optional OnCompleted. No notifications of any type should follow either an OnError or an OnCompleted.

这篇关于RX.Net:使用重试,但记录任何异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆