简体   繁体   中英

How to Debug a .NET Core Console App When Using dotnet run

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?

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM