System.Diagnostics.Debug.WriteLine being stepped over - c#

I am using Visual Studio 2013 in an ASP.NET MVC/C# project.
I am outputting to the output window using
System.Diagnostics.Debug.WriteLine
but for some reason VS has stopped running this line. I am executing in Debug mode. If I step through the code the debugger will jump over this line.
Is this controlled by a setting. I have searched the web but nothing obvious jump out

Go to project settings and see if there is check mark in front of Define DEBUG constant. Most likely that it's not there, although it should be by default.
If DEBUG constant is not defined - the method you are referring to is removed by compiler. That's why your debugger acts like code is not there.

Related

How to debug ASP.Net MVC in Visual Studio 2019

I was hunting a phantom-problem, until I recognized that the debugging wasn't working as expected.
I tried to debug some controller-code (c#) in my ASP.Net application and for this, I stopped the excecution, using a breakpoint (F9).
I started debugging with F5 and the code stopped, as expected at the breakpoint.
There, I tried to fetch some data, using different approaches.
None of the tries gave me the expected result so after a while, I recognized, that with every excecution the same compiled code is used and not the code, I was watching (and changing) in the editor.
I'm pretty sure, that in VS2017 it worked that way, so I could change the code while excecution.
How can I debug the C# code (and change the code) while excecution?
Thanks
You can do it in Vs2019 by In Tools > Options > (or Debug > Options) > Debugging > General, select Edit and Continue in the right pane.

visual studio: How to ignore pdb not loaded warnings during debug?

While debugging, I do not want visual studio to be looking for *.pdb files from 3rd party *.dlls. For example I use Jetbrains Resharper TaskRunner for unit testing. To use it requires a reference to Jetbrains.Resharper.TaskRunnerFramework.dll. When I debug my application via the TaskRunner, I end up with something similar to this picture below:
Well, I'm not debugging the task runner. That isn't my code anyhow. I want Visual Studio to be "smart" enough (or have a setting) to automatically know to NOT concern itself with *.pdb files for anything that isn't my code. Is this possible?
Have a look at the Just My Code setting in Visual Studio. From the documentation, there's a section that sounds relevant, which discusses one of the effects of having this setting applied:
When you Step Into (Keyboard shortcut: F11) non-user code, the debugger steps over the code to the next user statement. When you Step Out (Keyboard: Shift + F11), the debugger runs to the next line of user code. If no user code is encountered then execution continues until the app exits, a breakpoint is hit, or an exception occurs.
You can enable Just My Code in the Visual Studio options under Debugging > General:
Add a .gitignore file to your solution and add all extensions you want to ignore
Add this in your .gitignore file *.pdb

How to debug .NET console application from Visual Studio 2013

I've created a .NET 4.5 console application (an .exe file) with c# that processes some data in various files. The whole thing runs in less than one second. I have a problem with a testing version of the .exe that I'd like to step through in the Visual Studio 2013 debugger using particular input files (the filenames are passed as command line arguments). I can't do an "Attach to process..." since the process has completed too quickly to attach to it. How do I debug in this case.
Note that I'm from the python world, and the python equivalent of what I'm trying to do is python -m pdb pdb_script.py.
Thanks for any advice!
Right click on your project and go to "properties".
Then select the "debugging" tab on the left-hand side.
There's a box where you can input the desired command line arguments for use when running in debug mode.
EDIT: If you're asking how to start the debugger, then add some breakpoints to your code, then right click on the project in your solution and pick Debug > Start New Instance.
Alternatively you can right click and choose "Set as Startup Project", after which you can start debugging with F5 or the "Start" button at the top of the UI.
Add the following line in your code:
System.Diagnostics.Debugger.Break();
This will allow you to debug your application before it ends.
In Visual Studio, you can add commandline arguments before starting a debugging session by right-clicking your project, selecting properties and then going down to the debug tab.
Then you can start it in Visual Studio with f5
If that's too much trouble (because you are going to change the arguments a lot), you could do something like adding a Console.ReadLine to your program at the beginning that will give you a change to attach a debugger. You could even have an extra command line argument for debugging that will only pause for you to attach the debugger if you pass that argument.

C# can't start debugging application in IDE

I have C# application built in VS2010. Recently I changed something in project properties and now I have strange problem:
I can't start debugging of my application (F5). When I press F5 project compiles but then nothing happens. Even if I put some dummy code to my program.cs main function ie MessageBox.Show() it's not executed.
I can build app using "Build solution", it compiles and executes with no problems
In project properties following options are enabled:
- Define debug consant
- Define trace constant
- Advanced bulid settings / Debug info - full
What should I change to be able to debug my app from IDE?
Try deleting .suo file. Make backup first!
Here is similar thread:
VS2010, F5 - Builds but doesn't run (WPF)
Shutdown and restart VS :) It's happened to me a few times before.

Breakpoints not getting hit while debugging in VS10

I am working on a C# and Silverlight project and every once in a while I run into an issue where my breakpoints are no longer getting hit when I debug. In the editor they are not turning transparent so I know the correct code is loaded and being run.
An example would be:
I have Value with a getter and setter and it is bound to a control. When I put a breakpoint in the setter and I change the value of Value from the control the breakpoint is not getting hit.
I know that an IIS reset fixes this problem but I would like to know the cause. Does anybody else find similar behavior? If anybody would be able to point me in the direction of a possible cause that would be much appreciated.
There is an option in Visual Studio 2010:
Tools -> Options...
Debugging -> General
"Step over properties and operators (Managed only)"
Make sure this isn't checked. This assumes that the breakpoint is a solid red circle, indicating that VS has found debugging symbols for it.
Alternatively, these elements of code could be decorated with one of various debugging attributes, namely DebuggerStepThroughAttribute, DebuggerNonUserCodeAttribute and DebuggerHiddenAttribute. These may stop the debugger from going into the method, even if a breakpoint is there.
Of course, if the code you are debugging has been optimised, it may look like it's missing lines. I'm not sure what happens if you try to breakpoint a line that has been optimised away.
If the breakpoint has gone hollow (not solid red), then Visual Studio likely cannot find the debugging symbols for the code.
If a reset fixes the issue, perhaps there are differences between the code being debugged and the original source file / symbols, there is an option to make this less strict:
Same options area as above.
"Require source files to exactly match the original version"
Many times i face this problem though while on winforms apps. So simple thing i do is, restart VS prior to Clean and Rebuild the solution. Then if nothing works out, just delete the bin directory and let rebuild again. The last option i do is restart machine.
I've had this problem recently. While I did not find the exact cause, a simple fix was to check the app is running in debug mode (as opposed to release) and clean / rebuild the solution.
Found this question while trying to understand why my own project's breakpoints weren't being hit while trying to run code in vs2010
Solved it by looking at the project properties under Advanced Compile Options and setting Generate Debug Info to Full.
Might be worth mentioning I jump into code I wish to debug by using the very handy TestDriven.net's "Test With -> Debugger" via a right click on the function I wish to debug.
(At least) Two potential causes: Visual Studio options, or ReSharper options
Example: if I break at some call like Console.WriteLine(myVar.myProp), and I also break inside the myProp getter, the breakpoint inside the getter will be completely skipped if the following settings are still turned on.
For the Visual Studio Options:
And for the ReSharper Options:
So, turn these off to avoid datatips from causing your breakpoints to be skipped.

Categories