Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

About Debug

Status
Not open for further replies.

jack1080

Programmer
Jun 2, 2007
34
0
0
MY
Is it possible when we can change the solution configuration from Debug and Release, we make certain chunk of code ignore by the compiler,
which also means those chunk of code only run in Debug environment?
 
That is actually a really good question, I'm also interested in this.
 
you can use the #if DEBUG #end if blocks.
Code:
string DoSomething()
{
   string foo = "foo";
#if DEBUG
   Console.Write(foo);
#end if
   return foo;
}
I would use this sparingly, personally i find it makes the actual code harder to read.

i prefer to use a logging library like nlog or log4net (MS included one with EntLib, but i find it's too heavy). you simply add a line like this
Code:
string DoSomething()
{
   string foo = "foo";
   Log.Debug(foo);
   return foo;
}
the libraries above come with some form of filtering so you can choose which messages to acutally log at run time (config file). by changing the configs between debug and release you can filter different messages.

typically the log levels are debug(0), trace(1), information(2), warning(3), error(4). the lower the level the sooner the message is ignored.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
A lot of people have good luck with Log4Net - I always end up writing my own which spits out to an encrypted log file which I can then have my C# app email to me :)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top