Exemplo n.º 1
0
        public static void Capture(Action action, int expectedLength, out string output, out string errorOutput)
        {
            TextWriter errorOutputWriter = new CappedStringWriter(expectedLength);
            TextWriter outputWriter      = new CappedStringWriter(expectedLength);

            lock (ConsoleGuard)
            {
                TextWriter originalOut   = Console.Out;
                TextWriter originalError = Console.Error;
                try
                {
                    Console.SetOut(outputWriter);
                    Console.SetError(errorOutputWriter);
                    action();
                }
                finally
                {
                    Console.SetOut(originalOut);
                    Console.SetError(originalError);
                }
            }

            output      = outputWriter.ToString();
            errorOutput = errorOutputWriter.ToString();
        }
Exemplo n.º 2
0
        public static void CaptureOutput(Action action, int expectedLength, out string output, out string errorOutput)
        {
            var outputWriter      = new CappedStringWriter(expectedLength);
            var errorOutputWriter = new CappedStringWriter(expectedLength);

            var savedOutput = s_currentOut.Value;
            var savedError  = s_currentError.Value;

            try
            {
                s_currentOut.Value   = outputWriter;
                s_currentError.Value = errorOutputWriter;
                action();
            }
            finally
            {
                s_currentOut.Value   = savedOutput;
                s_currentError.Value = savedError;
            }

            output      = outputWriter.ToString();
            errorOutput = errorOutputWriter.ToString();
        }