Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of console.log in C#

Tags:

c#

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?

like image 675
Unknown developer Avatar asked Feb 21 '18 10:02

Unknown developer


People also ask

What can we use instead of console log ()?

warn() Probably the most obvious direct replacement for log() , you can just use console. warn() in exactly the same way. The only real difference is the output is a bit yellow.

Is console log same as return?

console. log() is a function used to print information to the console. return on the other hand is a call to pass some value back up to where the call was made.

What language has console log?

The console. log() is a function in JavaScript which is used to print any kind of variables defined before in it or to just print any message that needs to be displayed to the user. Syntax: console.

What is console log in programming?

console. log specifically is a method for developers to write code to inconspicuously inform the developers what the code is doing. It can be used to alert you that there's an issue, but shouldn't take the place of an interactive debugger when it comes time to debug the code.


3 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.

like image 79
Patrick Hofman Avatar answered Oct 11 '22 20:10

Patrick Hofman


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.

like image 41
Daniel Danielecki Avatar answered Oct 11 '22 19:10

Daniel Danielecki


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)

like image 29
TheHitchenator Avatar answered Oct 11 '22 19:10

TheHitchenator