By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,667 Members | 1,616 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,667 IT Pros & Developers. It's quick & easy.

How can I test if code is running in debug mode?

P: n/a
Is there any way besides adding a specific debug command line argument for
the project to tell if an application is running in debug mode?

Thanks!
Bill
Nov 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
You can test against conditional directives to get your code to tell you
what mode its in

private void Test()
{
#if DEBUG
Console.WriteLine("In Debug..");
#endif
}

See here:
http://msdn.microsoft.com/library/de...ml/vclrfif.asp

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

"Wysiwyg" <wy*****@xmissionNSPAM.com> wrote in message
news:d5**********@news.xmission.com...
Is there any way besides adding a specific debug command line argument for
the project to tell if an application is running in debug mode?

Thanks!
Bill

Nov 17 '05 #2

P: n/a
I think you can also use:

public void Foo1()
{
If (Debugger.IsAttached)
Foo2();
}

or

[Conditional("DEBUG")]
public void Foo1()
{
Foo2();
}
Nov 17 '05 #3

P: n/a
Be careful! System.Diagnostics.Debugger.IsAttached and #if DEBUG (or
the equivalent [Conditional("DEBUG")] ) have different effects! It
depends upon what you want!

#if DEBUG

and

[Conditional("DEBUG")]

tell the compiler to include the code (or the method) only for a _Debug
build_. So, if you compile the code under the Debug configuration (see
Configuration Manager under the Build menu), then the code will be
included. If you compile the code under the Release configuration then
the code will not be included.

It does not matter whether you run the Debug configuration under
debugger control, or run it from the command line. Code included using
#if DEBUG will exist and execute no matter how the application is run.
Similarly, the Release assembly will not include the code, so no matter
how it is run the code will never execute (because it's not compiled
in).

System.Diagnostics.Debugger.IsAttached produces a different effect: the
code is always compiled in, in all configurations. However, it runs
_only if_ the application is running under control of the debugger.
Even if you compile the Debug configuration, if you run it from the
command line (with no debugger), code within an

if (System.Diagnostics.Debugger.IsAttached)
{
... code here ...
}

will not execute, because the executable is not running under debugger
control.

Nov 17 '05 #4

P: n/a
Thanks to both of you!

Bill

"John Timney (ASP.NET MVP)" <ti*****@despammed.com> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
You can test against conditional directives to get your code to tell you
what mode its in

private void Test()
{
#if DEBUG
Console.WriteLine("In Debug..");
#endif
}

See here:
http://msdn.microsoft.com/library/de...ml/vclrfif.asp
--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director
"ESPNSTI" <ES*********@Hotmail.com> wrote in message
news:eE**************@TK2MSFTNGP09.phx.gbl... I think you can also use:

public void Foo1()
{
If (Debugger.IsAttached)
Foo2();
}

or

[Conditional("DEBUG")]
public void Foo1()
{
Foo2();
}

Nov 17 '05 #5

P: n/a
Bruce,

Thanks for the clarification.

"Bruce Wood" <br*******@canada.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Be careful! System.Diagnostics.Debugger.IsAttached and #if DEBUG (or
the equivalent [Conditional("DEBUG")] ) have different effects! It
depends upon what you want!

#if DEBUG

and

[Conditional("DEBUG")]

tell the compiler to include the code (or the method) only for a _Debug
build_. So, if you compile the code under the Debug configuration (see
Configuration Manager under the Build menu), then the code will be
included. If you compile the code under the Release configuration then
the code will not be included.

It does not matter whether you run the Debug configuration under
debugger control, or run it from the command line. Code included using
#if DEBUG will exist and execute no matter how the application is run.
Similarly, the Release assembly will not include the code, so no matter
how it is run the code will never execute (because it's not compiled
in).

System.Diagnostics.Debugger.IsAttached produces a different effect: the
code is always compiled in, in all configurations. However, it runs
_only if_ the application is running under control of the debugger.
Even if you compile the Debug configuration, if you run it from the
command line (with no debugger), code within an

if (System.Diagnostics.Debugger.IsAttached)
{
... code here ...
}

will not execute, because the executable is not running under debugger
control.

Nov 17 '05 #6

P: n/a
one correction, methods marked with ConditionalAttribute are always included
in the compiled dll. only the calling code is omitted depending on the
symbol defined. otherwise, methods like Debug.XXX and Trace.XXX would've
never made it into the BCL.

"Bruce Wood" wrote:
Be careful! System.Diagnostics.Debugger.IsAttached and #if DEBUG (or
the equivalent [Conditional("DEBUG")] ) have different effects! It
depends upon what you want!

#if DEBUG

and

[Conditional("DEBUG")]

tell the compiler to include the code (or the method) only for a _Debug
build_. So, if you compile the code under the Debug configuration (see
Configuration Manager under the Build menu), then the code will be
included. If you compile the code under the Release configuration then
the code will not be included.

It does not matter whether you run the Debug configuration under
debugger control, or run it from the command line. Code included using
#if DEBUG will exist and execute no matter how the application is run.
Similarly, the Release assembly will not include the code, so no matter
how it is run the code will never execute (because it's not compiled
in).

System.Diagnostics.Debugger.IsAttached produces a different effect: the
code is always compiled in, in all configurations. However, it runs
_only if_ the application is running under control of the debugger.
Even if you compile the Debug configuration, if you run it from the
command line (with no debugger), code within an

if (System.Diagnostics.Debugger.IsAttached)
{
... code here ...
}

will not execute, because the executable is not running under debugger
control.

Nov 17 '05 #7

P: n/a
Search for: Ollydbg.exe An excellent debugger of which version 10 or later
has been posted. From the "readme"..."OllyDbg 1.10 is a 32-bit
assembler-level analyzing debugger for
Microsoft(R) Windows(R) with intuitive interface."
"http://home.t-online.de/home/Ollydbg"
"Wysiwyg" wrote:
Is there any way besides adding a specific debug command line argument for
the project to tell if an application is running in debug mode?

Thanks!
Bill

Nov 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.