14

So I'm working on a custom dotnet cli tool as described here. I'm getting started with it and can run my console app using dotnet run, but it's going right past my breakpoints when trying to debug. It does work when running from VS, but I want to be able to play around with passing various arguments and doing that from the application arguments box is not really practical. Any thoughts?

3
  • You should be able to attach a debugger to an existing .net process, alternatively if timing is an issue, you can use Debugger.Launch learn.microsoft.com/en-us/dotnet/api/… Commented Jul 26, 2020 at 17:04
  • Your tool is still a console application. You can debug it just like any other console application, directly from your IDE whatever that is (VS, VS Code, Rider). You can pass whatever arguments you want in the Debug project property page Commented Jul 26, 2020 at 17:38
  • 1
    Debugging it by starting it from the debugger (i.e. Visual Studio) is still the best way to debug your application. Yes, changing the arguments through the project properties is a bit annoying but that’s still how to do it. You can also check out the launchSettings.json which gets modified through the project properties and change the arguments there directly if that seems nicer to work with for you. Commented Jul 26, 2020 at 18:20

3 Answers 3

6

You have multiple options:

  1. Debugger.Launch This is a function which will pop up a window where you can attach a Visual Studio Debugger. See: Debugger.Launch. This has one theoretical downside (which does not apply to you) it is only usable in Visual Studio and not for example in Rider as the API is not open. (But you when this window pops up you could attach Rider to the process)

  2. "Wait" the first seconds in your program You could just pass an argument to your cli which indicates it should wait x seconds so that you can attach your Debugger.

public static void Main(string[] args)
{
    if(args[0] == "waitfordebugger")
    {
        Thread.Sleep(10000); // Wait 10 Seconds
    }

    // Do stuff here

You then call your program like this: dotnet run -- waitfordebugger

Sign up to request clarification or add additional context in comments.

3 Comments

Both of these mean changing the application that is being debugged though so this can have unwanted side effects.
Doesn't this apply to other projects than console ones? I'm running an API and get nothing on the breakpoints. I added Debugger.Launch() in the Startup.cs and executed with the flag dotnet run --waitfordebugger. No breakies hit, though. The log tells me that the spot is passed but VS gives nothing orange blinking. Is attaching to a process the only way to go? Not sure how to do that...
Correction to the above. I know how to attach to a process, obviously. I meant, I want to do that without scrolling like a crazy money through the list of all processes. I'd like to attach to "right" process.
3

For what it's worth, it seems like the best way is unfortunately to pass them in as arguments.

You can do this by clicking on the arrow next to Run button in Visual Studio and selecting 'Project Debug Properties'. From there, you can go to 'Application Arguments' and enter the arguments you want. For example, something like --list all would pass in an array with a length of two where index 0 is --list and index 1 is all.

If someone comes up with a less invasive way, let me know!

Edit: You can also do it from command prompt/powershell by using dotnet run and attaching to the associated process in VS (Debug>Attach to Process)

Comments

0

There is the implementation of the debugger call in the MsBuild repo, DebuggerLaunchCheck function.
It is more convenient to attach to "right" process.
Stop occurs if a certain environment variable is set.
src/MSBuild/XMake.cs#L603

   Process currentProcess = Process.GetCurrentProcess();
   Console.WriteLine($"Waiting for debugger to attach 
   ({currentProcess.MainModule.FileName} PID {currentProcess.Id}).  Press 
   enter to continue...");
   Console.ReadLine();
   break;

Launch:

  Set CONSOLEDEBUGONSTART="2"
  dotnet run

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.