1

I am working on method in a DLL. In my method I have been writing to the event log to allow me to determine whether things are working as I expect. One of the tasks I do in the method is create a process and run a command line application and I want to be able to capture the output of this that is normally written to the console and write it to the eventlog.

Code:

Process getNextIons = new Process();
getNextIons.StartInfo.FileName = @"""C:\Program Files\OpenMS-1.6\PrecursorIonSelector.exe""";
getNextIons.StartInfo.Arguments = @"-ini ""C:\Program Files\OpenMS-1.6\precursorionselector.ini""";
getNextIons.StartInfo.UseShellExecute = false;
getNextIons.StartInfo.RedirectStandardOutput = true;
getNextIons.Start();
getNextIons.WaitForExit();
System.Diagnostics.EventLog.WriteEntry("FMANWiff", "IPS: " + getNextIons.StandardOutput.ReadToEnd());

I have a console test application that calls my method and when I do this I am able to see that the process was started and ran correctly, however when I actually try to make use of the DLL, not using the test application, all I end up seeing is an entry:

IPS: And none of the output. I can tell it is running, however, as I can see a number of output files being updated.

Why am I not getting any output and how I can rectify this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Travis
  • 11
  • 2

2 Answers2

1

According to the Process.StandardOutput documentation your code has a potential deadlock.

When the caller reads from the redirected stream of a child process, it is dependent on the child. The caller waits on the read operation until the child writes to the stream or closes the stream. When the child process writes enough data to fill its redirected stream, it is dependent on the parent. The child process waits on the next write operation until the parent reads from the full stream or closes the stream. The deadlock condition results when the caller and child process wait on each other to complete an operation, and neither can proceed.

To avoid the possible deadlock, the last two lines of your sample code should be switched. You should also consider redirecting StandardError.

Dan Busha
  • 3,723
  • 28
  • 36
1

In your library, instead of writing directly to the location of choice you should be using System.Diagnostics.Trace. By using Trace you can have your outside console application or whatever it be subscribe to the Trace event using a TraceListener.

By using Trace and TraceListeners, you can make you application adaptable to whatever logging situation you require without having to modify your library every time you want to change the logging system.

Below is a link to another Stack Overflow thread about trace logging with some good examples and information.

How can I add (simple) tracing in C#?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Josh DeLong
  • 475
  • 6
  • 24
  • Sadly I do not have access to the application that calls my library, it is closed source, they have basically provided a hook into their software, but I cannot go into that software and add tracing. – Travis Sep 20 '11 at 05:03
  • What operating system are you running this on? Reason I ask is that Windows Server 2008 and Vista have some new permissions about writing to the event log and if the process running the application doesn't have permissions then you won't get your output. What you can try doing is writing your output to a text file in the All Users area or ProgramData area where permissions won't be a problem. – Josh DeLong Sep 20 '11 at 13:56
  • I am using Windows XP for this. I will try writing to a file and see if that helps. The reason I would like to have this output is that when I test this using my test application, run via console, it works and I can see the output in the event log. But when it is actually running outside of this test (my method is called by a function in another dll that is called by some other software) platform the process does not seem to run correctly (it produces an output file but it is never created) but as I cannot get the output of the process I have no idea what is going wrong. – Travis Sep 21 '11 at 15:05
  • Could the fact that no console is ever open that causes this? – Travis Sep 21 '11 at 15:06