Using Console.WriteLine within a Windows Forms application

北城余情 提交于 2020-01-10 19:38:09

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/366345/using-console-writeline-within-a-windows-forms-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!