|
1 | | -using System; |
2 | | -using System.Threading.Tasks; |
3 | | -using Spectre.Console; |
4 | | -using Spectre.Console.Rendering; |
5 | | - |
6 | | -namespace WB.Logging.LogSinks.Console.Spectre; |
7 | | - |
8 | | -/// <summary> |
9 | | -/// Provides extension methods for the <see cref="ILogger"/> interface. |
10 | | -/// </summary> |
11 | | -public static class ILoggerExtensions |
12 | | -{ |
13 | | - // ┌─────────────────────────────────────────────────────────────────────────────┐ |
14 | | - // │ Public Methods │ |
15 | | - // └─────────────────────────────────────────────────────────────────────────────┘ |
16 | | - |
17 | | - /// <summary> |
18 | | - /// Attaches a <see cref="SpectreConsoleLogSink"/> to <paramref name="this"/> <see cref="ILogger"/>. |
19 | | - /// </summary> |
20 | | - /// <param name="this">The <see cref="ILogger"/> instance to attach the log sink to.</param> |
21 | | - /// <param name="configure">An optional action to configure the <see cref="SpectreConsoleLogSink"/> after it has been created and attached.</param> |
22 | | - /// <returns>An <see cref="IDisposable"/> that can be used to detach the log sink from the logger when it is no longer needed.</returns> |
23 | | - public static IDisposable AttachSpectreConsole(this ILogger @this, Action<SpectreConsoleLogSink>? configure = null) |
24 | | - { |
25 | | - ArgumentNullException.ThrowIfNull(@this); |
26 | | - |
27 | | - SpectreConsoleLogSink logSink = new(); |
28 | | - |
29 | | - IDisposable disposable = @this.AttachLogSink(logSink); |
30 | | - |
31 | | - configure?.Invoke(logSink); |
32 | | - |
33 | | - return disposable; |
34 | | - } |
35 | | - |
36 | | - /// <summary> |
37 | | - /// Logs the <paramref name="widget"/> as a log message. This method allows you to log any |
38 | | - /// Spectre.Console widget directly by passing it as an argument. |
39 | | - /// </summary> |
40 | | - /// <remarks>The <paramref name="widget"/> is logged with a <c>null</c> <see cref="LogLevel"/>.</remarks> |
41 | | - /// <param name="this">The <see cref="ILogger"/> instance to log the widget to.</param> |
42 | | - /// <param name="widget">The Spectre.Console widget to log.</param> |
43 | | - /// <returns>The same <see cref="ILogger"/> instance to allow for method chaining.</returns> |
44 | | - /// <seealso cref="IRenderable"/> |
45 | | - public static ILogger Widget(this ILogger @this, IRenderable widget) |
46 | | - { |
47 | | - ArgumentNullException.ThrowIfNull(@this); |
48 | | - |
49 | | - @this.Log(null, new WidgetPayload(widget)); |
50 | | - |
51 | | - return @this; |
52 | | - } |
53 | | - |
54 | | - /// <summary> |
55 | | - /// Logs a horizontal rule to the console. |
56 | | - /// </summary> |
57 | | - /// <param name="this">The <see cref="ILogger"/> instance to log the horizontal rule to.</param> |
58 | | - /// <param name="title">An optional title to display in the center of the horizontal rule.</param> |
59 | | - /// <returns>The same <see cref="ILogger"/> instance to allow for method chaining.</returns> |
60 | | - public static ILogger HorizontalRule(this ILogger @this, string? title = null) |
61 | | - { |
62 | | - ArgumentNullException.ThrowIfNull(@this); |
63 | | - |
64 | | - if (title is null) |
65 | | - { |
66 | | - return @this.Widget(new Rule()); |
67 | | - } |
68 | | - else |
69 | | - { |
70 | | - @this.Widget(new Rule(title)); |
71 | | - } |
72 | | - |
73 | | - return @this; |
74 | | - } |
75 | | - |
76 | | - /// <summary> |
77 | | - /// Starts a progress with the specified <paramref name="title"/> and <paramref name="progress"/> function. |
78 | | - /// The progress will be automatically completed when the <paramref name="progress"/> function completes. |
79 | | - /// </summary> |
80 | | - /// <param name="this">The <see cref="ILogger"/> instance to start the progress on.</param> |
81 | | - /// <param name="title">The title of the progress.</param> |
82 | | - /// <param name="progress">The function that performs the progress.</param> |
83 | | - /// <returns>A task that represents the asynchronous operation.</returns> |
84 | | - public static async Task StartProgressAsync(this ILogger @this, string title, Func<ProgressContext, Task> progress) |
85 | | - { |
86 | | - ArgumentNullException.ThrowIfNull(@this); |
87 | | - |
88 | | - ProgressPayload progressPayload = new() |
89 | | - { |
90 | | - Title = title, |
91 | | - Progress = progress, |
92 | | - }; |
93 | | - |
94 | | - await @this.FlushAsync().ConfigureAwait(false); |
95 | | - |
96 | | - @this.Log(null, progressPayload); |
97 | | - |
98 | | - await progressPayload.Completed.ConfigureAwait(false); |
99 | | - } |
100 | | -} |
| 1 | +using System; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using Spectre.Console; |
| 4 | +using Spectre.Console.Rendering; |
| 5 | + |
| 6 | +namespace WB.Logging.LogSinks.Console.Spectre; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// Provides extension methods for the <see cref="ILogger"/> interface. |
| 10 | +/// </summary> |
| 11 | +public static class ILoggerExtensions |
| 12 | +{ |
| 13 | + // ┌─────────────────────────────────────────────────────────────────────────────┐ |
| 14 | + // │ Public Methods │ |
| 15 | + // └─────────────────────────────────────────────────────────────────────────────┘ |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Attaches a <see cref="SpectreConsoleLogSink"/> to <paramref name="this"/> <see cref="ILogger"/>. |
| 19 | + /// </summary> |
| 20 | + /// <param name="this">The <see cref="ILogger"/> instance to attach the log sink to.</param> |
| 21 | + /// <param name="configure">An optional action to configure the <see cref="SpectreConsoleLogSink"/> after it has been created and attached.</param> |
| 22 | + /// <returns>An <see cref="IDisposable"/> that can be used to detach the log sink from the logger when it is no longer needed.</returns> |
| 23 | + public static IDisposable AttachSpectreConsole(this ILogger @this, Action<SpectreConsoleLogSink>? configure = null) |
| 24 | + { |
| 25 | + ArgumentNullException.ThrowIfNull(@this); |
| 26 | + |
| 27 | + SpectreConsoleLogSink logSink = new(); |
| 28 | + |
| 29 | + IDisposable disposable = @this.AttachLogSink(logSink); |
| 30 | + |
| 31 | + configure?.Invoke(logSink); |
| 32 | + |
| 33 | + return disposable; |
| 34 | + } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Logs the <paramref name="widget"/> as a log message. This method allows you to log any |
| 38 | + /// Spectre.Console widget directly by passing it as an argument. |
| 39 | + /// </summary> |
| 40 | + /// <remarks>The <paramref name="widget"/> is logged with a <c>null</c> <see cref="LogLevel"/>.</remarks> |
| 41 | + /// <param name="this">The <see cref="ILogger"/> instance to log the widget to.</param> |
| 42 | + /// <param name="widget">The Spectre.Console widget to log.</param> |
| 43 | + /// <returns>The same <see cref="ILogger"/> instance to allow for method chaining.</returns> |
| 44 | + /// <seealso cref="IRenderable"/> |
| 45 | + public static ILogger Widget(this ILogger @this, IRenderable widget) |
| 46 | + { |
| 47 | + ArgumentNullException.ThrowIfNull(@this); |
| 48 | + |
| 49 | + @this.Log(null, new WidgetPayload(widget)); |
| 50 | + |
| 51 | + return @this; |
| 52 | + } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Logs a horizontal rule to the console. |
| 56 | + /// </summary> |
| 57 | + /// <param name="this">The <see cref="ILogger"/> instance to log the horizontal rule to.</param> |
| 58 | + /// <param name="title">An optional title to display in the center of the horizontal rule.</param> |
| 59 | + /// <returns>The same <see cref="ILogger"/> instance to allow for method chaining.</returns> |
| 60 | + public static ILogger HorizontalRule(this ILogger @this, string? title = null) |
| 61 | + { |
| 62 | + ArgumentNullException.ThrowIfNull(@this); |
| 63 | + |
| 64 | + if (title is null) |
| 65 | + { |
| 66 | + return @this.Widget(new Rule()); |
| 67 | + } |
| 68 | + else |
| 69 | + { |
| 70 | + @this.Widget(new Rule(title)); |
| 71 | + } |
| 72 | + |
| 73 | + return @this; |
| 74 | + } |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// Starts a progress with the specified <paramref name="title"/> and <paramref name="progress"/> function. |
| 78 | + /// The progress will be automatically completed when the <paramref name="progress"/> function completes. |
| 79 | + /// </summary> |
| 80 | + /// <param name="this">The <see cref="ILogger"/> instance to start the progress on.</param> |
| 81 | + /// <param name="title">The title of the progress.</param> |
| 82 | + /// <param name="progress">The function that performs the progress.</param> |
| 83 | + /// <returns>A task that represents the asynchronous operation.</returns> |
| 84 | + public static async Task StartProgressAsync(this ILogger @this, string title, Func<ProgressContext, Task> progress) |
| 85 | + { |
| 86 | + ArgumentNullException.ThrowIfNull(@this); |
| 87 | + |
| 88 | + ProgressPayload progressPayload = new() |
| 89 | + { |
| 90 | + Title = title, |
| 91 | + Progress = progress, |
| 92 | + }; |
| 93 | + |
| 94 | + await @this.FlushAsync().ConfigureAwait(false); |
| 95 | + |
| 96 | + @this.Log(null, progressPayload); |
| 97 | + |
| 98 | + await progressPayload.Completed.ConfigureAwait(false); |
| 99 | + } |
| 100 | +} |
0 commit comments