Beispiel #1
0
        public static IServiceCollection AddThreaxProcessHelper(this IServiceCollection services, Action <ThreaxProcessHelperOptions>?configure = null)
        {
            var options = new ThreaxProcessHelperOptions();

            configure?.Invoke(options);

            services.TryAddScoped <IProcessRunner>(
                s => CreateRunner(s, options));

            return(services);
        }
Beispiel #2
0
        private static IProcessRunner CreateRunner(IServiceProvider s, ThreaxProcessHelperOptions options)
        {
            IProcessRunner runner = new ProcessRunner();

            if (options.IncludeLogOutput)
            {
                try
                {
                    var logger = s.GetRequiredService <ILogger <DefaultLog> >();
                    runner = new LoggingProcessRunner <DefaultLog>(runner, logger);
                }
                catch (ObjectDisposedException)
                {
                    //Sometimes this is called after the context is disposed.
                    //If that happens it is ok, logging will not be included.
                }
            }
            if (options.DecorateProcessRunner != null)
            {
                runner = options.DecorateProcessRunner.Invoke(runner);
            }
            return(runner);
        }