43

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.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • Would the down voters mind commenting please. I am happy to improve the question to help others in the future. – Linda Lawton - DaImTo Jan 29 '18 at 07:41
  • Does this answer your question? [Is there a way to detect if a debugger is attached to another process from C#?](https://stackoverflow.com/questions/2188201/is-there-a-way-to-detect-if-a-debugger-is-attached-to-another-process-from-c) –  May 13 '21 at 16:17

3 Answers3

70

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.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • 1
    I would suggest you to fix your remark about the `HttpContext` since you obviously missed that this question is about ASP.NET Core which usually *is* a console application. The reason why `HttpContext.Current` does not work is a different one. – poke Jan 27 '18 at 02:15
  • 2
    You are right. Often people mix .NET Standard and .NET Core up, so I was thinking OP did too. I updated my answer. – Patrick Hofman Jan 27 '18 at 06:59
  • @poke your right. I always forget that. If you are interested it's code from my identity server. I am still new to .net core – Linda Lawton - DaImTo Jan 29 '18 at 07:41
20

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.

poke
  • 369,085
  • 72
  • 557
  • 602
12

If you want to run a piece of code in Debug and not in Release, you can use the #if preprocessor directive.

#if(DEBUG)
  debug code here...
#else
  release code here...
#endif

If you don't want to run anything in the release version of the build, just don't include the else block.

Devined
  • 186
  • 1
  • 9
  • 5
    This is for DEBUG vs RELEASE builds and gets checked at compile time, not runtime. There is a huge difference between a debugger attached and build with debug symbols. – Linda Lawton - DaImTo Feb 02 '18 at 07:14