Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 949
Paul DeVito Avatar asked Feb 14 '26 03:02

Paul DeVito


1 Answers

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

like image 154
Link Avatar answered Feb 15 '26 17:02

Link



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!