views:

73

answers:

4

The aim is to be able to switch debugging calls on at run-time from database on a production build ...

+1  A: 

Not at the pre-processor level. After all, you're running the result of that process. Of course, you could change your pre-processor checks to be normal code checks, which can obviously be switched as required.

Rowland Shaw
A: 

You obviously can't affect the binary code as such (as with the #if statement).

What I usually do is to make sure that my code contains a lot of logging, that is activated by trace switches. That way you can have a system run in production with little or no logging, and when you want to research some problem you can switch logging on by altering the config file.

Fredrik Mörk
+2  A: 

No; the point of conditional methods and preprocessor directives is that they cause the compiler to omit code from the final executable. The runtime equivalent of these is an if statement.

However, aspect-orientated programming is roughly equivalent to what you're asking, in that it allows you to inject code into a running program that wouldn't otherwise be there.

Edit: as Joe mentioned, the way to do this is to program against a logging framework like log4net that allows fine-grained control over what gets logged.

Tim Robinson
+1  A: 

No, but most logging subsystems provide this ability. E.g. with log4net you can change the logging level dynamically, or if using System.Diagnostics.Trace you can change the TraceSwitch level dynamically.

Joe