Solved

C# Redirect Console Output to log file

Posted on 2007-03-22
9
2,294 Views
Last Modified: 2007-12-19
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.FileName = @"c:\nunit-console.exe";
            myprocess.StartInfo.WorkingDirectory = @"c:\" ;
            myprocess.StartInfo.Arguments= "MyTest.dll -s -m";
            myprocess.StartInfo.UseShellExecute = false;
            myprocess.StartInfo.RedirectStandardOutput = true;
            myprocess.StartInfo.CreateNoWindow = true;
            StreamWriter sw = new StreamWriter(@"c:\outfile.log");
            myprocess.Start();
            sw.WriteLine(myprocess.StandardOutput.ReadToEnd());
            sw.Close();
0
Comment
Question by:jrwalker2
  • 6
  • 2
9 Comments
 
LVL 16

Expert Comment

by:AlexNek
ID: 18769655
I remember that setting RedirectStandardOutput to true is not enough. In addition you must set
UseShellExecute = false;
http://msdn2.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput.aspx
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 18769656
Ohh you you have it already, sorry.
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 18769658
Try to remove myprocess.StartInfo.CreateNoWindow = true;
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 18769664
myprocess.WaitForExit(); can be useful too.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 9

Accepted Solution

by:
bele04 earned 250 total points
ID: 18769697
you should read the stream being returned by the process before calling the WaitForExit:

Process myprocess  = new Process();
            myprocess.StartInfo.FileName = @"c:\nunit-console.exe";
            myprocess.StartInfo.WorkingDirectory = @"c:\" ;
            myprocess.StartInfo.Arguments= "MyTest.dll -s -m";
            myprocess.StartInfo.UseShellExecute = false;
            myprocess.StartInfo.RedirectStandardOutput = true;
            myprocess.StartInfo.CreateNoWindow = true;
            StreamWriter sw = new StreamWriter(@"c:\outfile.log");
            myprocess.Start();
            string strOutput = myprocess.StandardOutput.ReadToEnd();
            myprocess.WaitForExit();
            sw.WriteLine(strOutput);
            sw.Close();

more info at the msdn site:
http://msdn2.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx
0
 
LVL 1

Author Comment

by:jrwalker2
ID: 18772793
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.Arguments = "/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.


0
 
LVL 16

Assisted Solution

by:AlexNek
AlexNek earned 250 total points
ID: 18774443
It is correct
myprocess.StartInfo.Arguments= "MyTest.dll -s -m";
you can try to write
myprocess.StartInfo.Arguments= "\"MyTest.dll -s -m\"";

0
 
LVL 1

Author Comment

by:jrwalker2
ID: 18775965
Thank you all
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 18776248
Dont mention it. Thank you too.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Hovering effect 9 30
DataGridview Currency Formating? 22 39
Hide Tab Page 3 20
Round up to 100% in .NET 10 46
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now