ofer
ofer

Reputation: 4476

Using Console.WriteLine within a Windows Forms application

I have an external DLL whose source code is C#. From the documentation for the DLL, I determined that it writes its debug messages to the console using Console.WriteLine.

I'd like to use this DLL within a WinForms application. However, what I have discovered is that I cannot see the debug messages emitted by the DLL since a WinForms application does not have a console.

is there a way to capture those debug messages, perhaps even to a simple log file? Of course, using ProcessInfo.RedirectStandartOutput will not work as I do not use the DLL as a process.

Upvotes: 6

Views: 2756

Answers (2)

user46119
user46119

Reputation: 81

You would be best served using the System.Diagnostics namespace and Debug.WriteLine instead. Debug supports 'listeners' that can be added at run-time or via the app/web.config files. For example:-

  Debug.Listeners.Add(new ConsoleTraceListener())

If you implement any custom debug logging as a trace listener, you can capture your trace messages application wide very easily.

Upvotes: 4

Jon Skeet
Jon Skeet

Reputation: 1499880

Call Console.SetOut with a TextWriter you control (e.g. a StringWriter).

Upvotes: 14

Related Questions