Search code examples
c#visual-studiodebuggingvisual-studio-2022tracepoint

Can I see on debug / breakpoint / tracepoint on how much times a method run using Visual Studio 2022


I have a custom attribute class. The custom attribute runs on every request. Since my requests are async. I want to know how many times my method on custom attribute runs.

Is there a way to count the execution of method or create a trace point on Visual Studio 2022?

I tried to look the breakpoint settings. On actions, I put {hitcount} as a message output window. But did not work.


Solution

  • As @stuartd said, if you want to count the execution of method or create a trace point, you can add a counter to your code. For detailed steps, please check the following:

    1.Create a static variable outside the method. Increment it every time method gets called.

     public static int counter = 0;
     public void myMethod()
     {
         Interlocked.Increment(ref counter);
     }
    

    2.Set tracepoints in your method. Enter the message {counter} into the Show a message in the Output Window text box.

    enter image description here

    Test result

    enter image description here

    Doc Referred:

    Set tracepoints in source code