views:

188

answers:

2

I have an exception that I need to swallow (exception during logging), however I dont want the exception information to be completely lost to the mists of time and so I figured I may as well at least output it to debug using

Debug.Write(ex.ToString());

That way if it becomes necessary support can at least use DebugView on the machine having problems.

Trouble is the Debug class is removed in release mode - how do I output something to debug whilst in release mode?

+11  A: 

Simply use

Trace.Write(ex.ToString());

This does the same as Debug.Write(ex.ToString()); but won't be removed in Release mode (as long as you didn't remove the definition of the TRACE constant in your project settings)

0xA3
Remember to rotate your log files after ~14 days to avoid filling the hard disk.
JBRWilkinson
+2  A: 

Maybe it would be worth looking into some logging framework. I prefer log4net where you have different logging levels (DEBUG, INFO, WARN, ERROR), different loggers (you can put one logger for each important part of the application), and you can set different debug levels for different loggers just by changing the config file. So if you have a problem in some area of the code you can set DEBUG level for that logger(s) and when you're done you can put the original level back.

This just scratches the surface, there are many more features, like sending email on errors, or logging to DB.

rslite