c# Production code running DEBUG version - c#

I have the following code:
#if (DEBUG)
imgPath = GetDirectoryName(Application.ExecutablePath);
#else
imgPath = GetDirectoryName(Application.ExecutablePath) + "\\images\\";
#endif
When the code went into Production (live site) , it still looked the the DEBUG version. How is this possible? Is there something during the promotion process that can indicate do RELEASE vs DEBUG

You might have deployed a version with binaries taken from a Debug build configuration or in any case, the DEBUG variable set to True.
You need to build in RELEASE and use that output as the release candidate.

When using #if statements, it is checking to determine if the constant is defined. Under the project's properties->Build section, the DEBUG constant is (by default) set to be defined when in debug mode. There is no RELEASE constant unless you define it yourself. You may have used your build from debug mode or had the "Define DEBUG constant" checked for your release build. Double check which you used and your build settings.
Also, your code should be greyed out to indicate that VS will not be compiling that code. So if the code you wish to be using is grey, that is an indicator you have DEBUG defined somewhere.

Related

#if DEBUG and Conditional(“DEBUG”) called in Release build

I have a method that sets up some Debug only configurations, I have used this pattern on a few projects and with some of them it seems that nor #if DEBUG nor Conditional("DEBUG") are omitted in my release build.
Any ideas why?
Define DEBUG constant is checked:
As stated by #Evk, ""Define DEBUG constant" is checked. You need to uncheck it".
That defines DEBUG for the preprocessor even if it is not in the text box above it
Something that may be obvious for some: the Build configuration changes based on the selected Configuration (eg. Debug) and Platform (eg. Any CPU).
In order to have #if DEBUG and Conditional("DEBUG") working as expected, the DEBUG constant must be defined in the 'Debug' configuration only!
Notice the difference:
Debug:
Release:
If DEBUG is defined in Release, when using #if DEBUG or Conditional("DEBUG"), the condition will return true because the DEBUG constant exists in the project configuration.

#if DEBUG doesn't seem to work in my VS 2010

I've been doing the following in my C# code:
#if DEBUG
//Debugger only code
#endif
But for some reason when I do the same in my C# console application (that is built as a Windows service in VS 2010) it doesn't seem to work and for both Release and Debug configurations it looks like DEBUG is not defined.
Do I need to set something for that preprocessor constant to work?
When using the 'Debug' configuration, you have to go to project's Properties -> Build
and tick the 'Define DEBUG constant' box.
Either that, or define the conditional compilation symbol: DEBUG. As you can do with any other custom compilation symbol :)

How to leave certain lines out of build using visual studio?

I am new to C# and Visual Studio, and this may be a silly question but I do have to find an answer. My question is is it possible to differentiate debug and release builds in such a way the certain lines of code(in my case MessageBox.Show(...)) are compiled if I build under Debug mode, but are left out if I build under Release mode?
Wrap the lines of code in #if DEBUG statements:
#if DEBUG
MessageBox.Show("Message");
#endif
If you want to do different things in release then you can have:
#if DEBUG
MessageBox.Show("Message");
#else
// Write to log file
#endif
Source
you can use the compiler instruction "#if DEBUG" followed by "#endif" so your code will be compiled only on debug mode.
find more details here:
http://msdn.microsoft.com/en-us/library/4y6tbswk.aspx
In addition to #if DEBUG, you can also use the Assert static method on the Debug class in System.Diagnostics to check things and show a message box if the value is wrong. e.g:
Debug.Assert(IsValid);
Will show a message in the debug build if IsValid is false.
You can specify different conditional compilation symbols in the project's properties (an article here; MSDN documentation for Visual Studio). Then, using conditional compilation it's easy to include/exclude certain lines. Like this:
#if DEBUG
MessageBox.Show(...);
#end
Yes, you can use
#if DEBUG
MessageBox.Show....
#endif
If you check build version, you find you have variables defined in project: DEBUG is only defined in debug release.
If you want you can also switch the behaviour using
#if DEBUG
MessageBox.Show....
#else
// do something else here in release mode
#endif

#If DEBUG is ignored (VB.net or C#)

I have several of these in my code which have been working fine so far:
#If DEBUG Then
... some code here
#End If
Now, i am noticing that, lately, the code inside the " #If DEBUG Then ... #End If"
gets execute also in "Release Mode".
This is strange and did not happen before. What could have happened so that
the #If DEBUG are now being ignored (they are ignored both in debug in the IDE or the final executable) ?
I have applied Clean, Rebuild, etc.: no luck. Thank you for any hints and help.
-Pam
Firstly, make sure you understand the difference between how you're running the code and how you're building it. Too many people equate "launching in a debugger" with "the debug version" and "launching not in a debugger" with "the release version". They're completely orthogonal - you can launch a release build in a debugger (typically with less information available) and you can launch a debug build not in a debugger. Apologies if you were already aware of this.
Now, assuming you really have changed the project configuration you're building to Release, you need to check the project properties for that specific configuration. I don't know what it looks like in VB, but in C# in the project properties, in the build tab, there will be a list of defined symbols - that is what affects whether #if DEBUG code is built or not. Perhaps someone has copied over the project configuration from Debug into Release?
EDIT: One way to check this at build time is:
#if DEBUG
#error This shouldn't happen
#endif
In a release build, that should build without error. In debug, it won't.
EDIT: Another option is that your overall solution configuration is now referring to the wrong project configuration types. I can't remember the exact menu name, but if you look around Project for Configuration Manager, you should be able to bring up a grid mapping "Project" and "Solution Configuration" to the project configuration to build.
C# Project ( Visual Studio )
go to: Project Properties -> Build(tab)
Select Configuration: Release
Uncheck "Define DEBUG constant"
Now select Configuration: Debug
Check "Define DEBUG constant"
In your code, you can now type the following ( DEBUG with uppercase )
#IF DEBUG
// Debugging code goes here
#ENDIF
Under Project Properties / Compile / Advanced Compile Options there is
a checkbox called "Define Debug Constant" that sets this.
Check out: http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_24658238.html
EDIT: Try this initializing with this:
#If CONFIG = "Debug" Then
#CONST DEBUG = true
#if CONFIG = "Release" Then
#CONST DEBUG = false
Did you, by any chance, tick the "Define DEBUG constant" for the Release configuration, while you were in the Project Properties / Build?
Also make sure you are not building the project-level Debug configuration within the solution-level Release configuration (see the Configuration Manager).
Also remember #if DEBUG must be in uppercase. e.g. #if debug won't work.
Had a similar problem where "DEBUG" was never true. Tried by doing an uncheck and check of the "Define DEBUG constant" checkbox and rebuilding everytime but that did not work.
My solution was to define "DEBUG" manually in the "Conditional compilation symbols" textbox for the Debug configuration. When rebuilding, Visual Studio 2019 automatically removed the DEBUG symbol from the textbox (because this indeed should not be there) and from then on it worked again. When i switched from Debug to Release the correct lines got greyed out.
This seems to be a possible bug in VS 2019 (16.4.5)?
undefine DEBUG and that will not execute that portion.
If you are using ASP.NET make sure about this line in Web.Config file:
<compilation debug="false" targetFramework="4.5">
So if debug="true" your project runs in DEBUG mode.
If you are using VB.NETFramework v4.5 then use like
If Debugger.IsAttached Then
'... some code here
End If

Will #if RELEASE work like #if DEBUG does in C#?

In all the examples I've seen of the #if compiler directive, they use "DEBUG". Can I use "RELEASE" in the same way to exclude code that I don't want to run when compiled in debug mode? The code I want to surround with this block sends out a bunch of emails, and I don't want to accidentally send those out when testing.
RELEASE is not defined, but you can use
#if (!DEBUG)
...
#endif
No, it won't, unless you do some work.
The important part here is what DEBUG really is, and it's a kind of constant defined that the compiler can check against.
If you check the project properties, under the Build tab, you'll find three things:
A text box labelled "Conditional compilation symbols"
A check box labelled "Define DEBUG constant"
A check box labelled "Define TRACE constant"
There is no such checkbox, nor constant/symbol pre-defined that has the name RELEASE.
However, you can easily add that name to the text box labelled Conditional compilation symbols, but make sure you set the project configuration to Release-mode before doing so, as these settings are per configuration.
So basically, unless you add that to the text box, #if RELEASE won't produce any code under any configuration.
Nope.
While in debug configuration there is a DEBUG defined constant (automatically defined by Visual Studio) while there is no such constant defined for release mode. Check your project settings under build.
Selecting [Define DEBUG constant] under Project -> Build is like including #define DEBUG at the beginning of every file.
If you want to define a RELEASE constant for the release configuration go to:
Project Properties -> Build
Select Release Mode
in the Conditional compilation symbols textbox enter: RELEASE
On my VS install (VS 2008) #if RELEASE does not work. However you could just use #if !DEBUG
Example:
#if !DEBUG
SendTediousEmail()
#endif
I've never seen that before...but I have seen:
#if (DEBUG == FALSE)
and
#if (!DEBUG)
That work for ya?
You can use #if(!DEBUG) for this purposes.
"Pop Catalin" got it right. Controlling the definition based on the type of build provides a great deal of flexibility. For example, you can have a "DEBUG", "DEMO", and "RELEASE" configuration all in the same solution. That prevents the need for duplicate programming with two different solutions.
So yes #if RELEASE or #if (RELEASE) works the same as #if DEBUG when the RELEASE Conditional compilation symbol is defined.
The following is taken from "Pop Catalin" post:
If you want to define a RELEASE constant for the release configuration go to:
* Project Properties -> Build
* Select Release Mode
* in the Conditional compilation symbols textbox enter: RELEASE
I know this is an old question, but it might be worth mentioning that you can create your own configurations outside of DEBUG and RELEASE, such as TEST or UAT.
If then on the Build tab of the project properties page you then set the "Conditional compilation symbols" to TEST (for instance) you can then use a construct such as
#if (DEBUG || TEST )
//Code that will not be executed in RELEASE or UAT
#endif
You can use this construct for specific reason such as different clients if you have the need, or even entire Web Methods for instance. We have also used this in the past where some commands have caused issues on specific hardware, so we have a configuration for an app when deployed to hardware X.
You can create you own conditional compile-time symbols (any name you like).
Go to the "project Build dialog", located in the project properties box,
menu option: Project->[projectname] Properties...
You can also define them "at the top of the C# code file". Like:
#define RELEASE
// or
#undef RELEASE
you can use the symbol in a #if statement:
#if RELEASE
// code ...
#elif …
// code ...
#endif
// or
#if !RELEASE
// code ...
#endif
Whilst M4N's answer (#if (!DEBUG)) makes most sense, another option could be to use the preprocessor to amend other flag's values; e.g.
bool isRelease = true;
#if DEBUG
isRelease = false;
#endif
Or better, rather than referring to whether we're in release or debug mode, use flags that define the expected behavior and set them based on the mode:
bool sendEmails = true;
#if DEBUG
sendEmails = false;
#endif
This is different to using preprocessor flags, in that the flags are still there in production, so you incur the overhead of if (sendEmails) {/* send mails */} each time that code's called, rather than the code existing in release but not existing in debug, but this can be advantageous; e.g. in your tests you may want to call your SendEmails() method but on a mock, whilst running in debug to get additional output.
Another option:
#If CONFIG = "Release" Then
....
#End If
why not just
#if RELEASE
#undef DEBUG
#endif

Categories

Resources