BryanWilhite / SonghayCore

core reusable, opinionated concerns for *all* 🧐 of my C# projects
http://songhayblog.azurewebsites.net/
MIT License
1 stars 0 forks source link

consider supporting `ILogger` for proposed extensions of `IActivity` #75

Closed BryanWilhite closed 4 years ago

BryanWilhite commented 4 years ago

continues on from https://github.com/BryanWilhite/Songhay.HelloWorlds.Activities/issues/1

this would mean adding Microsoft.Extensions.Logging.Abstractions as a dependency to SonghayCore

and turning these into extension methods:

internal static async Task<TOutput> StartActivityAsync(TInput input, IActivity activity, ILogger log)
{
    using (var writer = new StringWriter())
    using (var listener = new TextWriterTraceListener(writer))
    {
        traceSource.Listeners.Add(listener);

        string output = null;

        try
        {
            var activityOutput = activity.ToIActivityOutput<TInput, TOutput>();
            output = await activityOutput.StartAsync(input);
        }
        finally
        {
            listener.Flush();
            log?.LogInformation(writer.ToString());
        }

        return output;
    }
}

and

internal static void StartActivity(ProgramArgs args, IActivity activity, ILogger log)
{
    using (var writer = new StringWriter())
    using (var listener = new TextWriterTraceListener(writer))
    {
        traceSource.Listeners.Add(listener);

        try
        {
            activity.Start(args);
        }
        finally
        {
            listener.Flush();
            log?.LogInformation(writer.ToString());
        }
    }
}
BryanWilhite commented 4 years ago

i have decided to rename IActivityOutput to IActivityWithOutput so this interface will not be confused with the new model ActivityOutput<TOutput>

this is another breaking change of mine that will not be recognized by my version numbering ☹️

this new model ActivityOutput<TOutput> will eliminate the need for taking a dependency on ILogger at the expense of complexity