I am running an MVC Web application built by using Visual Studio 2017. I want to test some parts of C# code without using debugger breakpoints. Is there any way to know a certain part of code has run by adding some code at that point? Something like console.log in Javascript? As to make things more specific I'd like to add this "watcher code" inside the following method:
public static void Main(string[] args)
{ BuildWebHost(args).Run();
}Is the above doable and how?
33 Answers
Trace.WriteLine will do (and probably more methods from the Trace class).
You can use a program called DebugView to real-time monitor the trace generated, or write to a log file using you web.config file.
Try using System.Diagnostics.Debug.WriteLine("This is a log"); and in Visual Studio open View and then in Output you will see the log when running your application.
Console.WriteLine will do it provided that you have a console to view it in (for a forms application this works when debugging in something like visual studio, but the end user will never see the output)