Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if debugging

I have a .Net Core console application. In the configure method in my startup.cs I am trying to test if the debugger is enabled or not:

 if (HttpContext.Current.IsDebuggingEnabled)      loggerFactory.AddConsole(Configuration);  else      loggerFactory.AddConsoleJson(Configuration); 

HttpContext.Current.IsDebuggingEnabled doesn't seam to be supported in .Net core. I havent been able to find a method that works in .net core.

System.Diagnostics.DebuggableAttribute.DebuggingModes.Default doesn't appear to work either.

like image 906
DaImTo Avatar asked Jan 26 '18 12:01

DaImTo


People also ask

How do I run debug mode?

Run the program in debug modeClick the Run icon in the gutter, then select Modify Run Configuration. Enter arguments in the Program arguments field. Click the Run button near the main method. From the menu, select Debug.

What happens in debug mode?

Running an app within a debugger, also called debugging mode, means that the debugger actively monitors everything that's happening as the program runs. It also allows you to pause the app at any point to examine its state and then step through your code line by line to watch every detail as it happens.

What is debug mode and release mode?

In Debug Mode your .exe has debug information inside of it (source code, variable names and other similar stuff like that). In Release Mode your .exe lack of debug information makes it smaller and probably performs better due to its smaller footprint.

How do I enable debugging in Visual Studio?

In the Visual Studio toolbar, make sure the configuration is set to Debug. To start debugging, select the profile name in the toolbar, such as <project profile name>, IIS Express, or <IIS profile name> in the toolbar, select Start Debugging from the Debug menu, or press F5.


2 Answers

HttpContext is another HttpContext than you were used to since you are now using ASP.NET Core. That property can't be found in the ASP.NET Core variant. An explanation of the differences is given by poke.

I would use Debugger.IsAttached, which not only checks if debugging is enabled, but also actively being debugged.

like image 136
Patrick Hofman Avatar answered Sep 29 '22 07:09

Patrick Hofman


HttpContext.Current refers to System.Web.HttpContext which is part of the System.Web namespace that was used in the old ASP.NET.

ASP.NET Core does not use types within the System.Web namespace, and anything that applied in the old ASP.NET world will need to be reevaluated whether it still works with the appropriate new types in the Microsoft.AspNetCore namespace.

For the HttpContext, the new type is Microsoft.AspNetCore.Http.HttpContext. However, it does not have a IsDebuggingEnabled property that the old type had.

The reason for this is that the old ASP.NET was an application that was running inside a web server (most commonly IIS), and that web server provided the HttpContext to the application. So you had to use HttpContext.Current to access that context.

In ASP.NET Core however, the application includes the webserver, making ASP.NET Core applications able to run completely independent of this. Now, when debugging, instead of attaching to the parent webserver, with ASP.NET Core, you are now attaching to process of the ASP.NET Core application itself. This is also why you are usually creating command line applications (which contain the webserver with your application code). And because they are normal (command line) applications, you will have to use the standard tools to figure out whether or not the debugger is attached.

The usual way for this is to check Debugger.IsAttached for this:

if (Debugger.IsAttached) {     // debugger is attached } 

However, note that debuggers are not required to attach right when an application launches. It is perfectly fine to only attach the debugger later when the application is already running. This is important since the code in your Startup or your WebHostBuilder will only run once when the application starts. So it is possible that even with the debugger being attached, the code that registered your logging provider ran at a time where the debugger was not yet attached.

like image 32
poke Avatar answered Sep 29 '22 05:09

poke