2

Is it possible to write error messages to the console from process that has been fired up? Normally we are used to using Console.Writeline() from within the console. The additional process (in another assembly) is designed to be operated by the console, and potentially also through a GUI.


However if we attach methods to event handlers (i.e. a callback), then Console.WriteLine is not working.

Currently using:

var thread=new Thread(someMethod);
thread.Start();

Subsequently, callbacks may get fired off, it's here that console messages go missing.

sgtz
  • 8,849
  • 9
  • 51
  • 91

5 Answers5

2

Console.WriteLine() should work as well.

Update:

If you want to capture output of some console application, please take a look at Capturing console output from a .NET application (C#).

Community
  • 1
  • 1
2

If you're concerned about two streams of messages co-mingling, why not use Console.Error.WriteLine()? You can then redirect the error stream at execution to (for example) a file.

Randolpho
  • 55,384
  • 17
  • 145
  • 179
  • +1: ty. don't mind if two streams co-mingle. Just concerned that one stream is not showing up at all. Just want to see the info pop up in the console during execution right now. I've updated the question. – sgtz Feb 01 '12 at 17:41
1

I'm not quite sure if I understand this correctly. Do you want to read from the standard output of a child process and write in the parent's console? Then you can use the Process.StandardOutput property to get a StreamReader and start reading.

Tudor
  • 61,523
  • 12
  • 102
  • 142
  • It's a .net assembly that uses a 3rd party assembly. I'm wondering if the 3rd party .dll is sucking in console console data on the callback events. Is that possible? I updated the question. – sgtz Feb 01 '12 at 17:34
1

If you use the standard .NET tracing, you could configure a ConsoleTraceListener through the app.config: http://msdn.microsoft.com/en-us/library/system.diagnostics.consoletracelistener.aspx

This approach is explored more through this question: Redirect Trace output to Console

It's quite nice for diagnostic purposes as you mention. You can hook up text file writers, etc.

Community
  • 1
  • 1
jglouie
  • 12,523
  • 6
  • 48
  • 65
  • +1: ty. that's really handy info. Problem is that even Console.WriteLine is failing. I've updated the question a little. – sgtz Feb 01 '12 at 17:39
1

Is this an external, compiled process, i.e. an executable?

If you're using the Process class to start the process, you might be looking for the Process.StandardOutput property, which will let you redirect the text normally written to the console to another stream.

Otherwise, can you clarify what kind of "process" you're talking about?

qxn
  • 17,162
  • 3
  • 49
  • 72