Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i add a console output to a windows wpf application C#

i would like to add an additional console window to log realtine info from my wpf application. Any idea??

Bayo

answer: console application in the project properties works for me. thank's

like image 761
Bayo Alen Avatar asked Sep 26 '11 17:09

Bayo Alen


2 Answers

Don't do it.

Take a look at log4net or NLog for log output into a file. With the right configuration of those frameworks you get a lot more power (different log levels, automatic timestamps, automatic class names in front of every logged line)

And while you are at it, you might also want to implement a facade of your own, to hide the used logging framework from the rest of your code. This would allow you to easily change the logging framework, if and when the need arises.


If you want to have both a console and a GUI window for your program, you could implement this behaviour by compiling the project as console application (csc /target:exe). But beware: This most certainly leads to bad usability, because no user would expect your app to have both a console and a GUI window.

like image 128
yas4891 Avatar answered Sep 17 '22 13:09

yas4891


You could call AttachConsole WIN API function and then call this function using PInvoke:

[DllImport("kernel32.dll", SetLastError = true)]
static extern bool AttachConsole(uint dwProcessId);

const uint ATTACH_PARENT_PROCESS = 0x0ffffffff;  // default value if not specifing a process ID

// Somewhere in main method
AttachConsole(ATTACH_PARENT_PROCESS);
like image 30
Sergey Teplyakov Avatar answered Sep 19 '22 13:09

Sergey Teplyakov