enable disable option of console output in multithreaded applications

794 Views Asked by At

Hello I am working on a Server Application that is multithreaded having a control panel form and a console for the output. I am using log by console output. For Enabling log I compile the version with log and for disabling log I compile the version without log. This is handled as I use all code is between the lines.

#if Enable_DEBUG_ECHO
Console.WriteLine("Something is happening");
#endif

so I if use #define Enable_DEBUG_ECHO on top of file it compiles for echo. But I want implement a option by which user can enable/disable log by single click or something like that. Somebody give me the idea how can I do this.

2

There are 2 best solutions below

0
On

You can use a logging framework such as NLog or log4net, that makes all this so easy. You can configure them programatically or through configuration files.

0
On

You know, when u are using precompile options (such as your code), the program contains only code one 'version' of code (I mean, that if you define Enable_DEBUG_ECHO, line 'Console.WriteLine("Something is happening");' will included in result dll, but it will not included if you don't define Enable_DEBUG_ECHO).

So if u want to enable/disable your log in runtime, u should create your log without precompile options.

So u need to save your 'turn on/off' variable in configuration file. And than

if (VARIABLE_FROM_CONFIG)
{
Console.WriteLine("Something is happening");
}