I have a couple of sections of code that are very processor intensive. I have debug statements in there for internal deployments and testing but when I deploy t开发者_JAVA百科o my customer sites I would like to do a build that doesn't have these. I currently use:
ifIsDebugEnabled()
to wrap the log messages but this is also taking up processor time and when I commented all the logging out the performance improved. Is there a standard way to go about this or are there other things I can do instead?
Thanks, Richard
To optimize out method calls, you can put the ConditionalAttribute
on methods. Like this:
[Conditional("DEBUG")]
void SomeDebugMethod(string message) {
// ...
}
Calls to this method will be removed if the code is compiled in another configuration than DEBUG.
This is a cleaner looking way than using directives like #ifdef DEBUG .. #endif
, if you can afford to put your Debug logic in Methods.
#if DEBUG
#endif
in your code to skip out code from the release build...
You could wrap your code with preprocessor directives like this:
#if DEBUG
// debug code
#endif
There's a complete list of available directives here
You can use conditional compilation:
#if DEBUG
// code to log here
#endif
The code will only be included in the compile if you're running DEBUG mode. You can also create your own conditional compilation directives using the project properties page.
You can use preprocessor directives to cause the debug mode check to happen at compile-time... so when you compile without DEBUG defined it will completely skip your debug code, causing no performance hit.
#define DEBUG
// ...
#if DEBUG
Console.WriteLine("Debug");
#endif
精彩评论