Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net Core, detecting debugging vs. not debugging in a controller

I am writing my first ASP.Net code web app and in my controller I would like to have an if statement that checks to see if I am in debugging mode or not. I know in the Startup.cs file I can check env.IsDevelopment() but that is because the IHostingEnvironment is passed into it. I have not been able to find a way to check for this status inside a normal controller. Is there a way in ASP.Net Core to detect when I am in debug mode inside the controller that I am missing?

like image 424
Matthew Verstraete Avatar asked Mar 14 '17 01:03

Matthew Verstraete


People also ask

How can I tell if debugging is enabled in asp net?

Debugging is enabled when the debug attribute in the compilation element is set to true. Change the debug attribute to false to disable debugging for that application. Save the Web. config file.

What is debugging in ASP NET?

Debugging allows the developers to see how the code works in a step-by-step manner, how the values of the variables change, how the objects are created and destroyed, etc.

What is debugging and tracing in C#?

Among the many diagnostic classes the . NET Framework provides are the Debug and Trace classes. The Debug class helps us debug code, and the Trace class helps us trace the execution of code. The Debug class is intended for debug builds, and the Trace class is used for release builds.


3 Answers

Update: @Pradeep Kumar's post below is the more correct answer here. This answer only indicates how to access the IsDevelopment() environment flag via dependency injection in a controller.

Update: IHostingEnvironment is obsolete in .Net Core 3.1 see the following for .Net Core 3.1+ https://stackoverflow.com/a/61703339/2525561

You should be able to just inject IHostingEnvironment into your controller constructor.

protected readonly IHostingEnvironment HostingEnvironment;

public TestController(IConfiguration configuration, IHostingEnvironment hostingEnv){
    this.Configuration = configuration;
    this.HostingEnvironment = hostingEnv;
}

[HttpGet]
public IActionResult Test(){
    if(this.HostingEnvironment.IsDevelopment()){
        // Do something
    }

    return View();
}
like image 52
Cody Lohse Avatar answered Oct 19 '22 10:10

Cody Lohse


IHostingEnvironment lets you know the environment in which the application is running. Looks like what you need is the build configuration used to build the application i.e Debug/Release. In an ASP.NET Core web application, In order to get this information at compile time, there is no straight forward way, however you can have a property with conditional compilation using compiler directives, something like

public static bool IsDebug
{
  get
     {
      bool isDebug = false;
    #if DEBUG
       isDebug = true;
    #endif
       return isDebug;
      }
}

At runtime, you can check the value of IsDebug property to determine the build configuration. I would suggest to add this property to a common static or utility class which can be accessible from all your controllers.

like image 27
Pradeep Kumar Avatar answered Oct 19 '22 10:10

Pradeep Kumar


An alternative to my own answer, prompted by the comment by user @roblem, and based on an alternative reading of the OP's question, is as follows:

Managed Debuggers

To detect if a managed (.NET) debugger is actually attached to the process, one can use System.Diagnostics.Debugger.IsAttached property. This is useful during development when you need to programmatically determine if your code is running from the IDE or standalone.

using System.Diagnostics;

if (Debugger.IsAttached)
{
    // Debugger is attached, we are probably running from the IDE
}
else
{
    // We are probably running standalone
}

Note that the above only works for managed debuggers, such as the one built into the IDE.

Unmanaged Debuggers

To detect if an unmanaged debugger is attached, one needs to call CheckRemoteDebuggerPresent WinAPI function:

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

public class DetectDebugger
{
    [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
    static extern bool CheckRemoteDebuggerPresent(IntPtr hProcess, ref bool isDebuggerAttached);

    public static void Main()
    {
        bool isDebuggerAttached = false;
        CheckRemoteDebuggerPresent(Process.GetCurrentProcess().Handle, ref isDebuggerAttached);

        Console.WriteLine("Debugger is attached: " + isDebuggerAttached);
        // Prevent the console window from immediately closing:
        Console.ReadLine();
    }
}

Summary

In summary (found here):

  • CheckRemoteDebuggerPresent - works for any running process and detects native debuggers too.
  • Debugger.IsAttached - works only for the current process and detects only managed debuggers. As an example, OllyDbg won’t be detected by this.
like image 39
Optimax Avatar answered Oct 19 '22 11:10

Optimax