Greg Burghardt Greg Burghardt - 4 days ago 5
C# Question

How to prevent DEBUG preprocessor constant from being #DEFINE'd automatically when publishing from msbuild.exe

We have a C# web application that is being published using a Powershell script. In our C# code we have several places where we check

#if DEBUG
to conditionally compile the application with added support for logging.

The problem is the
DEBUG
constant is defined when we publish from the command line using this command:

cmd /c "path\to\msbuild.exe path\to\project.csproj /p:DeployOnBuild=true;PublishProfile=Development /verbosity:minimal"


We created the "Development" build configuration to correspond to our shared development environment, and then in Visual Studio through the GUI set the "Development" build configuration when creating the publish profile.

How do I prevent the
DEBUG
preprocessor constant from being automagically defined?


I must be missing something simple.

Answer

You need to specify the Configuration Property set to Development

/p:Configuration=Development

cmd /c "path\to\msbuild.exe path\to\project.csproj /p:Configuration=Development/p:DeployOnBuild=true;PublishProfile=Development /verbosity:minimal"

By default Visual Studio will create the projects with two configurations:

Debug: This one should be used for development, it includes the DEBUG constant by default. Release: This one should be used for production

You can also create custom configurations from scratch or based on Debug/Release, if you based a custom configuration "Development" in Debug, it will include the DEBUG constant, you can remove it manually from the .csproj file or right click the project and review the project properties.

Do not confuse the "Configuration" property with a PublishProfile, the PublishProfile is intended to create a Deployment package after compilation, you can use a PublishProfile combining it with Debug or Release.

If you are using web.config transformations be sure to update the "Release" or the custom configuration transform based on your needs.

Comments