Using Console.WriteLine within a Windows Forms app

2019-02-10 00:39发布

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.

2条回答
兄弟一词,经得起流年.
2楼-- · 2019-02-10 00:54

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.

查看更多
混吃等死
3楼-- · 2019-02-10 01:15

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

查看更多
登录 后发表回答