5

I need to add some logging to my app, and it needs to be in release mode, and I need to make sure that the logging code isn't running in production. Therefore I'd like to use a #define and #if to make sure the log statements are removed when I move to production.

Trouble is, I need the logging in multiple files, and it's a pain to put a #define at the top of every file. Is there a way to have a centralized #define? That way I can remove the single #define rather than a #define from all files(which means I'll almost assuredly forget one).

Jonathan Beerhalter
  • 7,229
  • 16
  • 68
  • 78
  • You may be better of by using dependency injection to use empty logger class in production over conditional compilation. Having different versions of binaries between final testing and production is not the best thing... – Alexei Levenkov Dec 14 '11 at 17:50

5 Answers5

9

On the command line, use the /define switch. In Visual Studio, choose the "Build" tab from the properties page for the project and fill in the "Conditional Compilation Symbols" section.

Consider also instead of using conditional compilation, to instead make your logging methods conditional methods. That's a more pleasant-looking alternative. That's how Debug.Assert works; it is a conditional method, so if the debug preprocessor symbol is not defined, the compiler simply removes all calls to the method before code generation.

See also my article on the subject:

http://ericlippert.com/2009/09/10/whats-the-difference-between-conditional-compilation-and-the-conditional-attribute/

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
2

Are you using Visual Studio? In the project Properties page, on the "Build" tab, there's a "Conditional compilation symbols" text box.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
1

Yes, this is typically done in your build file, or the script you use which creates your build. You specify it as command-line arguments to MSBuild.

Community
  • 1
  • 1
Dave Markle
  • 95,573
  • 20
  • 147
  • 170
1

To add to Dave's answer, global conditional compilation symbols can also be specified in Visual.

  1. Right-click on your project and go to Properties
  2. Go to the Build tab

You can specify the symbols that you like (DEBUG is already turned on by default for Debug configurations, so this might actually give you what you want already) for the given configuration, or select "All Configurations" at the top to specify certain symbols for all configurations.

Adam Robinson
  • 182,639
  • 35
  • 285
  • 343
1

Call the logging everywhere you want.

Define the logging api entry methods with

[Conditional ("DEBUG")]
public void WriteDebugMessage(...)

Build your program in debug mode (which, by default, defines 'DEBUG' in VS). These calls will be part of your assembly.

Build your program in release mode (or - remove the DEBUG symbol from the build definition). These calls are now meaningless no-ops and won't run.

Seems like what you want?

Benjamin Podszun
  • 9,679
  • 3
  • 34
  • 45