jrwalker2
asked on
C# Redirect Console Output to log file
I would like to run an executable program that takes an argument and then redirect the console output to a file. The following is how I normally do it from command line (runs nunit-console.exe, passes it MyTest.dll as an argument, and redirects the console output to outfile.log):
nunit-console.exe MyTest.dll > outfile.log 2>&1
I would like to be able to do this strictly from my C# code. The following is the code I have, but it does not work. Can you please help me (this is very urgent)???? Thank you so much!!!
Process myprocess = new Process();
myprocess.StartInfo.FileNa me = @"c:\nunit-console.exe";
myprocess.StartInfo.Workin gDirectory = @"c:\" ;
myprocess.StartInfo.Argume nts= "MyTest.dll -s -m";
myprocess.StartInfo.UseShe llExecute = false;
myprocess.StartInfo.Redire ctStandard Output = true;
myprocess.StartInfo.Create NoWindow = true;
StreamWriter sw = new StreamWriter(@"c:\outfile. log");
myprocess.Start();
sw.WriteLine(myprocess.Sta ndardOutpu t.ReadToEn d());
sw.Close();
nunit-console.exe MyTest.dll > outfile.log 2>&1
I would like to be able to do this strictly from my C# code. The following is the code I have, but it does not work. Can you please help me (this is very urgent)???? Thank you so much!!!
Process myprocess = new Process();
myprocess.StartInfo.FileNa
myprocess.StartInfo.Workin
myprocess.StartInfo.Argume
myprocess.StartInfo.UseShe
myprocess.StartInfo.Redire
myprocess.StartInfo.Create
StreamWriter sw = new StreamWriter(@"c:\outfile.
myprocess.Start();
sw.WriteLine(myprocess.Sta
sw.Close();
Ohh you you have it already, sorry.
Try to remove myprocess.StartInfo.Create NoWindow = true;
myprocess.WaitForExit(); can be useful too.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I tried the above code, but it is not working for me yet. It prints to the oufile.log what seems to be rules on how to run nunit-console.exe with arguments, but I do not know how to apply it. The MyTest.dll is a library of NUnit tests that I am trying to run. I also saw on the following website an example of running an .exe file and passing args to it it the way I think I want to, but I'm not sure how to apply it (compiler.StartInfo.Argume nts = "/r:System.dll /out:sample.exe stdstr.cs") .
http://msdn2.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput.aspx
Can you please provide code? Thank you
Below is what printed in the outfile.log:
-------------------------- ---------- -------
NUnit version 2.2.2
Copyright (C) 2002-2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole.
Copyright (C) 2000-2003 Philip Craig.
All Rights Reserved.
OS Version: Microsoft Windows NT 5.1.2600 Service Pack 2 .NET Version: 2.0.50727.42
NUNIT-CONSOLE [inputfiles] [options]
Runs a set of NUnit tests from the console.
You may specify one or more assemblies or a single
project file of type .nunit.
Options:
/fixture=STR Fixture to test
/config=STR Project configuration to load
/xml=STR Name of XML output file
/transform=STR Name of transform file
/xmlConsole Display XML to the console
/output=STR File to receive test output (Short format: /out=STR)
/err=STR File to receive test error output
/labels Label each test in stdOut
/include=STR List of categories to include
/exclude=STR List of categories to exclude
/noshadow Disable shadow copy
/thread Run tests on a separate thread
/wait Wait for input before closing console window
/nologo Do not display the logo
/help Display help (Short format: /?)
Options that take values may use an equal sign, a colon
or a space to separate the option from its value.
http://msdn2.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput.aspx
Can you please provide code? Thank you
Below is what printed in the outfile.log:
--------------------------
NUnit version 2.2.2
Copyright (C) 2002-2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole.
Copyright (C) 2000-2003 Philip Craig.
All Rights Reserved.
OS Version: Microsoft Windows NT 5.1.2600 Service Pack 2 .NET Version: 2.0.50727.42
NUNIT-CONSOLE [inputfiles] [options]
Runs a set of NUnit tests from the console.
You may specify one or more assemblies or a single
project file of type .nunit.
Options:
/fixture=STR Fixture to test
/config=STR Project configuration to load
/xml=STR Name of XML output file
/transform=STR Name of transform file
/xmlConsole Display XML to the console
/output=STR File to receive test output (Short format: /out=STR)
/err=STR File to receive test error output
/labels Label each test in stdOut
/include=STR List of categories to include
/exclude=STR List of categories to exclude
/noshadow Disable shadow copy
/thread Run tests on a separate thread
/wait Wait for input before closing console window
/nologo Do not display the logo
/help Display help (Short format: /?)
Options that take values may use an equal sign, a colon
or a space to separate the option from its value.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you all
Dont mention it. Thank you too.
UseShellExecute = false;
http://msdn2.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput.aspx