Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are sensible use-cases for Debugger.IsAttached Property

Tags:

c#

.net

debugging

the .Net property Debugger.IsAttached can be used at runtime to detect if a debugger is attached or not. That means this property can be used for having a different behavior when the debugger is attached. I can easily come up with examples that show why not to use this property (because the program will be hard to debug) but I have a hard time to find an example in which it seems legitimate to use it.

Do you have use-cases where Debugger.IsAttached is the sensible choice to use?

like image 640
codingdave Avatar asked Dec 12 '18 08:12

codingdave


People also ask

How to check if a debugger is attached to a process?

This is a dig into the Thread Information Block (TIB) of the process to check if a debugger is attached. And for those who don't already know, the FS register points to the current thread's TIB. Needless to say, coming to this involved a fair amount of disassemblies of Windows while in the debugger.

Does Microsoft have a warranty on the use of debugger?

Microsoft makes no warranties, express or implied, with respect to the information provided here. Gets a value that indicates whether a debugger is attached to the process. true if a debugger is attached; otherwise, false.

How to debug JavaScript in debugger?

The debugging can be turned on or off depending upon the user’s convenience. This all can be done through the “Console” of the debugger menu. There is another way in which the JS values can be displayed in the debugger window. 1.

What is the use of debugger keyword in C++?

The debugger keyword is used in the code to force stop the execution of the code at a breaking point and calls the debugging function. The debugger function is executed if any debugging is needed at all else no action is performed.


3 Answers

You can use that property in some scenarios to halt a service at a specific point when the debugger is attached to a remote server for example. Then that check would be accompanied by Debugger.Break.

Another use case might be some additional logging (containing sensitive or extensive logging) that is written to the console.

like image 139
Patrick Hofman Avatar answered Oct 02 '22 18:10

Patrick Hofman


I personally find it useful for logging. When debugger is attached then logging goes to the console window if not it goes though the Nlog setup which actually writes the logs out as json.

if (!Debugger.IsAttached)
    logging.AddNLog(NLogAspNetCoreOptions.Default);
else
    logging.AddConsole();
like image 41
DaImTo Avatar answered Oct 02 '22 19:10

DaImTo


A good usecase is using it on the startup of your application to start some services only if you have a debugger attached.

My case is using the Hangfire Dashboard.

Hangfire comes with a dashboard but you have to set it up on application startup, the dashboard is realtime and consumes resources that i don't need it to on a production environment as noone will need to see it only me when i am debugging. So i set the condition to fire up the dashboard only when i am in debug mode so i can see running jobs and their status and why they failed.

like image 38
npo Avatar answered Oct 02 '22 17:10

npo