| | 1 | | namespace Songhay.Extensions; |
| | 2 | |
|
| | 3 | | /// <summary> |
| | 4 | | /// Extensions of <see cref="IActivity"/> |
| | 5 | | /// </summary> |
| | 6 | | // ReSharper disable once InconsistentNaming |
| | 7 | | public static partial class IActivityExtensions |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Starts the <see cref="IActivity"/>. |
| | 11 | | /// </summary> |
| | 12 | | /// <param name="activity">The activity.</param> |
| | 13 | | /// <param name="args">The arguments.</param> |
| | 14 | | /// <param name="traceSource">The trace source.</param> |
| | 15 | | /// <returns>The <see cref="TraceSource"/> log.</returns> |
| | 16 | | public static string? StartActivity(this IActivity? activity, ProgramArgs? args, TraceSource? traceSource) => |
| 1 | 17 | | activity.StartActivity(args, traceSource, traceWriterGetter: null, flushLog: true); |
| | 18 | |
|
| | 19 | | /// <summary> |
| | 20 | | /// Starts the <see cref="IActivity"/>. |
| | 21 | | /// </summary> |
| | 22 | | /// <param name="activity">The activity.</param> |
| | 23 | | /// <param name="args">The arguments.</param> |
| | 24 | | /// <param name="traceSource">The trace source.</param> |
| | 25 | | /// <param name="traceWriterGetter">gets the <see cref="TextWriter"/> for the <see cref="TraceSource"/>.</param> |
| | 26 | | /// <param name="flushLog">when <c>true</c> return <see cref="TraceSource"/> log</param> |
| | 27 | | /// <returns>The <see cref="TraceSource"/> log when <c>flushLog</c> is <c>true</c>.</returns> |
| | 28 | | public static string? StartActivity(this IActivity? activity, ProgramArgs? args, TraceSource? traceSource, |
| | 29 | | Func<TextWriter>? traceWriterGetter, bool flushLog) |
| 2 | 30 | | { |
| 2 | 31 | | ArgumentNullException.ThrowIfNull(activity); |
| 2 | 32 | | ArgumentNullException.ThrowIfNull(args); |
| | 33 | |
|
| 2 | 34 | | using var writer = traceWriterGetter?.Invoke() ?? new StringWriter(); |
| 2 | 35 | | using var listener = new TextWriterTraceListener(writer); |
| | 36 | |
|
| 2 | 37 | | traceSource?.Listeners.Add(listener); |
| | 38 | |
|
| 2 | 39 | | string? log = null; |
| | 40 | |
|
| | 41 | | try |
| 2 | 42 | | { |
| 2 | 43 | | activity.Start(args); |
| 2 | 44 | | } |
| | 45 | | finally |
| 2 | 46 | | { |
| 2 | 47 | | if (flushLog) |
| 1 | 48 | | { |
| 1 | 49 | | listener.Flush(); |
| 1 | 50 | | log = writer.ToString(); |
| 1 | 51 | | } |
| 2 | 52 | | } |
| | 53 | |
|
| 2 | 54 | | return log; |
| 2 | 55 | | } |
| | 56 | |
|
| | 57 | | /// <summary> |
| | 58 | | /// Starts the <see cref="IActivity"/> |
| | 59 | | /// for the specified output (<typeparamref name="TOutput"/>). |
| | 60 | | /// </summary> |
| | 61 | | /// <param name="activity">The activity.</param> |
| | 62 | | /// <param name="input">The input.</param> |
| | 63 | | /// <param name="traceSource">The trace source.</param> |
| | 64 | | /// <returns><see cref="ActivityOutput{TOutput}"/></returns> |
| | 65 | | public static ActivityOutput<TOutput> StartActivityForOutput<TInput, TOutput>(this IActivity? activity, |
| | 66 | | TInput input, TraceSource? traceSource) => |
| 1 | 67 | | activity.StartActivityForOutput<TInput, TOutput>(input, traceSource, traceWriterGetter: null, |
| 1 | 68 | | flushLog: true); |
| | 69 | |
|
| | 70 | | /// <summary> |
| | 71 | | /// Starts the <see cref="IActivity"/> |
| | 72 | | /// for the specified output (<typeparamref name="TOutput"/>). |
| | 73 | | /// </summary> |
| | 74 | | /// <param name="activity">The activity.</param> |
| | 75 | | /// <param name="input">The input.</param> |
| | 76 | | /// <param name="traceSource">The trace source.</param> |
| | 77 | | /// <param name="traceWriterGetter">gets the <see cref="TextWriter"/> for the <see cref="TraceSource"/>.</param> |
| | 78 | | /// <param name="flushLog">when <c>true</c> return <see cref="TraceSource"/> log</param> |
| | 79 | | /// <returns>The <see cref="TraceSource"/> log when <c>flushLog</c> is <c>true</c>.</returns> |
| | 80 | | public static ActivityOutput<TOutput> StartActivityForOutput<TInput, TOutput>(this IActivity? activity, |
| | 81 | | TInput input, TraceSource? traceSource, Func<TextWriter>? traceWriterGetter, bool flushLog) |
| 1 | 82 | | { |
| 1 | 83 | | using var writer = traceWriterGetter?.Invoke() ?? new StringWriter(); |
| 1 | 84 | | using var listener = new TextWriterTraceListener(writer); |
| | 85 | |
|
| 1 | 86 | | traceSource?.Listeners.Add(listener); |
| | 87 | |
|
| 1 | 88 | | var activityOutput = new ActivityOutput<TOutput>(); |
| | 89 | |
|
| | 90 | | try |
| 1 | 91 | | { |
| 1 | 92 | | var activityWithOutput = activity.ToActivityWithOutput<TInput, TOutput>(); |
| | 93 | |
|
| 1 | 94 | | activityOutput.Output = activityWithOutput!.StartForOutput(input); |
| 1 | 95 | | } |
| | 96 | | finally |
| 1 | 97 | | { |
| 1 | 98 | | if (flushLog) |
| 1 | 99 | | { |
| 1 | 100 | | listener.Flush(); |
| 1 | 101 | | var log = writer.ToString(); |
| 1 | 102 | | activityOutput.Log = log; |
| 1 | 103 | | } |
| 1 | 104 | | } |
| | 105 | |
|
| 1 | 106 | | return activityOutput; |
| 1 | 107 | | } |
| | 108 | |
|
| | 109 | | /// <summary> |
| | 110 | | /// Starts the <see cref="IActivity"/>, asynchronously. |
| | 111 | | /// </summary> |
| | 112 | | /// <typeparam name="TInput">The type of the input.</typeparam> |
| | 113 | | /// <typeparam name="TOutput">The type of the output.</typeparam> |
| | 114 | | /// <param name="activity">The activity.</param> |
| | 115 | | /// <param name="input">The input.</param> |
| | 116 | | /// <param name="traceSource">The <see cref="TraceSource"/>.</param> |
| | 117 | | /// <returns><see cref="ActivityOutput{TOutput}"/></returns> |
| | 118 | | public static async Task<ActivityOutput<TOutput>> StartActivityAsync<TInput, TOutput>(this IActivity? activity, |
| | 119 | | TInput input, TraceSource? traceSource) => |
| 0 | 120 | | await activity |
| 0 | 121 | | .StartActivityAsync<TInput, TOutput>(input, traceSource, traceWriterGetter: null) |
| 0 | 122 | | .ConfigureAwait(continueOnCapturedContext: false); |
| | 123 | |
|
| | 124 | | /// <summary> |
| | 125 | | /// Starts the <see cref="IActivity"/>, asynchronously. |
| | 126 | | /// </summary> |
| | 127 | | /// <typeparam name="TInput">The type of the input.</typeparam> |
| | 128 | | /// <typeparam name="TOutput">The type of the output.</typeparam> |
| | 129 | | /// <param name="activity">The activity.</param> |
| | 130 | | /// <param name="input">The input.</param> |
| | 131 | | /// <param name="traceSource">The <see cref="TraceSource"/>.</param> |
| | 132 | | /// <param name="traceWriterGetter">gets the <see cref="TextWriter"/> for the <see cref="TraceSource"/>.</param> |
| | 133 | | /// <returns><see cref="ActivityOutput{TOutput}"/></returns> |
| | 134 | | public static async Task<ActivityOutput<TOutput>> StartActivityAsync<TInput, TOutput>(this IActivity? activity, |
| | 135 | | TInput input, TraceSource? traceSource, Func<TextWriter>? traceWriterGetter) |
| 0 | 136 | | { |
| 0 | 137 | | ArgumentNullException.ThrowIfNull(activity); |
| | 138 | |
|
| 0 | 139 | | await using var writer = traceWriterGetter?.Invoke() ?? new StringWriter(); |
| 0 | 140 | | using var listener = new TextWriterTraceListener(writer); |
| | 141 | |
|
| 0 | 142 | | traceSource?.Listeners.Add(listener); |
| | 143 | |
|
| 0 | 144 | | var activityOutput = new ActivityOutput<TOutput>(); |
| | 145 | |
|
| | 146 | | try |
| 0 | 147 | | { |
| 0 | 148 | | var activityWithOutput = activity.ToActivityWithTask<TInput, TOutput>(); |
| 0 | 149 | | activityOutput.Output = await activityWithOutput! |
| 0 | 150 | | .StartAsync(input) |
| 0 | 151 | | .ConfigureAwait(continueOnCapturedContext: false); |
| 0 | 152 | | } |
| | 153 | | finally |
| 0 | 154 | | { |
| 0 | 155 | | listener.Flush(); |
| 0 | 156 | | activityOutput.Log = writer.ToString(); |
| 0 | 157 | | } |
| | 158 | |
|
| 0 | 159 | | return activityOutput; |
| 0 | 160 | | } |
| | 161 | |
|
| | 162 | | /// <summary> |
| | 163 | | /// Starts the <see cref="IActivity"/>, asynchronously. |
| | 164 | | /// </summary> |
| | 165 | | /// <typeparam name="TInput">The type of the input.</typeparam> |
| | 166 | | /// <param name="activity">The activity.</param> |
| | 167 | | /// <param name="input">The input.</param> |
| | 168 | | /// <param name="traceSource">The <see cref="TraceSource"/>.</param> |
| | 169 | | /// <returns>The <see cref="TraceSource"/> log.</returns> |
| | 170 | | public static async Task<string?> StartActivityAsync<TInput>(this IActivity? activity, TInput input, |
| | 171 | | TraceSource? traceSource) => |
| 0 | 172 | | await activity |
| 0 | 173 | | .StartActivityAsync(input, traceSource, traceWriterGetter: null, flushLog: true) |
| 0 | 174 | | .ConfigureAwait(continueOnCapturedContext: false); |
| | 175 | |
|
| | 176 | | /// <summary> |
| | 177 | | /// Starts the <see cref="IActivity"/>, asynchronously. |
| | 178 | | /// </summary> |
| | 179 | | /// <typeparam name="TInput">The type of the input.</typeparam> |
| | 180 | | /// <param name="activity">The activity.</param> |
| | 181 | | /// <param name="input">The input.</param> |
| | 182 | | /// <param name="traceSource">The <see cref="TraceSource"/>.</param> |
| | 183 | | /// <param name="traceWriterGetter">gets the <see cref="TextWriter"/> for the <see cref="TraceSource"/>.</param> |
| | 184 | | /// <param name="flushLog">when <c>true</c> return <see cref="TraceSource"/> log</param> |
| | 185 | | /// <returns>The <see cref="TraceSource"/> log when <c>flushLog</c> is <c>true</c>.</returns> |
| | 186 | | public static async Task<string?> StartActivityAsync<TInput>(this IActivity? activity, TInput input, |
| | 187 | | TraceSource? traceSource, Func<TextWriter>? traceWriterGetter, bool flushLog) |
| 0 | 188 | | { |
| 0 | 189 | | ArgumentNullException.ThrowIfNull(activity); |
| | 190 | |
|
| 0 | 191 | | await using var writer = traceWriterGetter?.Invoke() ?? new StringWriter(); |
| 0 | 192 | | using var listener = new TextWriterTraceListener(writer); |
| | 193 | |
|
| 0 | 194 | | traceSource?.Listeners.Add(listener); |
| | 195 | |
|
| 0 | 196 | | string? log = null; |
| | 197 | |
|
| | 198 | | try |
| 0 | 199 | | { |
| 0 | 200 | | var activityWithTask = activity.ToActivityWithTask<TInput>(); |
| 0 | 201 | | await activityWithTask! |
| 0 | 202 | | .StartAsync(input) |
| 0 | 203 | | .ConfigureAwait(continueOnCapturedContext: false); |
| 0 | 204 | | } |
| | 205 | | finally |
| 0 | 206 | | { |
| 0 | 207 | | if (flushLog) |
| 0 | 208 | | { |
| 0 | 209 | | listener.Flush(); |
| 0 | 210 | | log = writer.ToString(); |
| 0 | 211 | | } |
| 0 | 212 | | } |
| | 213 | |
|
| 0 | 214 | | return log; |
| 0 | 215 | | } |
| | 216 | |
|
| | 217 | | /// <summary> |
| | 218 | | /// Starts the <see cref="IActivity"/>, asynchronously. |
| | 219 | | /// </summary> |
| | 220 | | /// <param name="activity">The activity.</param> |
| | 221 | | /// <param name="traceSource">The <see cref="TraceSource"/>.</param> |
| | 222 | | /// <returns>The <see cref="TraceSource"/> log.</returns> |
| | 223 | | public static async Task<string?> StartActivityAsync(this IActivity? activity, TraceSource? traceSource) => |
| 0 | 224 | | await activity |
| 0 | 225 | | .StartActivityAsync(traceSource, traceWriterGetter: null, flushLog: true) |
| 0 | 226 | | .ConfigureAwait(continueOnCapturedContext: false); |
| | 227 | |
|
| | 228 | | /// <summary> |
| | 229 | | /// Starts the <see cref="IActivity"/>, asynchronously. |
| | 230 | | /// </summary> |
| | 231 | | /// <param name="activity">The activity.</param> |
| | 232 | | /// <param name="traceSource">The <see cref="TraceSource"/>.</param> |
| | 233 | | /// <param name="traceWriterGetter">gets the <see cref="TextWriter"/> for the <see cref="TraceSource"/>.</param> |
| | 234 | | /// <param name="flushLog">when <c>true</c> return <see cref="TraceSource"/> log</param> |
| | 235 | | /// <returns>The <see cref="TraceSource"/> log when <c>flushLog</c> is <c>true</c>; otherwise, <c>null</c>.</returns |
| | 236 | | public static async Task<string?> StartActivityAsync(this IActivity? activity, TraceSource? traceSource, |
| | 237 | | Func<TextWriter>? traceWriterGetter, bool flushLog) |
| 0 | 238 | | { |
| 0 | 239 | | ArgumentNullException.ThrowIfNull(activity); |
| | 240 | |
|
| 0 | 241 | | await using var writer = traceWriterGetter?.Invoke() ?? new StringWriter(); |
| 0 | 242 | | using var listener = new TextWriterTraceListener(writer); |
| | 243 | |
|
| 0 | 244 | | traceSource?.Listeners.Add(listener); |
| | 245 | |
|
| 0 | 246 | | string? log = null; |
| | 247 | |
|
| | 248 | | try |
| 0 | 249 | | { |
| 0 | 250 | | var activityWithTask = activity.ToActivityWithTask(); |
| 0 | 251 | | await activityWithTask! |
| 0 | 252 | | .StartAsync() |
| 0 | 253 | | .ConfigureAwait(continueOnCapturedContext: false); |
| 0 | 254 | | } |
| | 255 | | finally |
| 0 | 256 | | { |
| 0 | 257 | | if (flushLog) |
| 0 | 258 | | { |
| 0 | 259 | | listener.Flush(); |
| 0 | 260 | | log = writer.ToString(); |
| 0 | 261 | | } |
| 0 | 262 | | } |
| | 263 | |
|
| 0 | 264 | | return log; |
| 0 | 265 | | } |
| | 266 | | } |