forked from reactiveui/ReactiveUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoggingMixins.cs
More file actions
72 lines (68 loc) · 3.03 KB
/
LoggingMixins.cs
File metadata and controls
72 lines (68 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System;
using System.Reactive.Linq;
using Splat;
namespace ReactiveUI
{
public static class ObservableLoggingMixin
{
/// <summary>
/// Logs an Observable to Splat's Logger
/// </summary>
/// <param name="klass">The hosting class, usually 'this'</param>
/// <param name="message">An optional method</param>
/// <param name="stringifier">An optional Func to convert Ts to strings.</param>
/// <returns>The same Observable</returns>
public static IObservable<T> Log<T, TObj>(this IObservable<T> This,
TObj klass,
string message = null,
Func<T, string> stringifier = null)
where TObj : IEnableLogger
{
message = message ?? "";
if (stringifier != null) {
return This.Do(
x => klass.Log().Info("{0} OnNext: {1}", message, stringifier(x)),
ex => klass.Log().WarnException(message + " " + "OnError", ex),
() => klass.Log().Info("{0} OnCompleted", message));
} else {
return This.Do(
x => klass.Log().Info("{0} OnNext: {1}", message, x),
ex => klass.Log().WarnException(message + " " + "OnError", ex),
() => klass.Log().Info("{0} OnCompleted", message));
}
}
/// <summary>
/// Like Catch, but also prints a message and the error to the log.
/// </summary>
/// <param name="klass">The hosting class, usually 'this'</param>
/// <param name="next">The Observable to replace the current one OnError.</param>
/// <param name="message">An error message to print.</param>
/// <returns>The same Observable</returns>
public static IObservable<T> LoggedCatch<T, TObj>(this IObservable<T> This, TObj klass, IObservable<T> next = null, string message = null)
where TObj : IEnableLogger
{
next = next ?? Observable<T>.Default;
return This.Catch<T, Exception>(ex => {
klass.Log().WarnException(message ?? "", ex);
return next;
});
}
/// <summary>
/// Like Catch, but also prints a message and the error to the log.
/// </summary>
/// <param name="klass">The hosting class, usually 'this'</param>
/// <param name="next">A Func to create an Observable to replace the
/// current one OnError.</param>
/// <param name="message">An error message to print.</param>
/// <returns>The same Observable</returns>
public static IObservable<T> LoggedCatch<T, TObj, TException>(this IObservable<T> This, TObj klass, Func<TException, IObservable<T>> next, string message = null)
where TObj : IEnableLogger
where TException : Exception
{
return This.Catch<T, TException>(ex => {
klass.Log().WarnException(message ?? "", ex);
return next(ex);
});
}
}
}