What is Console.Log Equivalent in C#?


I am working in .NET Core Web-application/API, while working, I want to print some variables output in "Output" Windows of Visual Studio, like in javascript we use Console.Log() what is similar way of using Console log in C# Web application or Console Application? Thanks


Asked by:- Vinnu
0
: 2719 At:- 11/30/2022 3:03:42 PM
C# c# print to debug console c# console log







1 Answers
profileImage Answered by:- vikas_jk

In C# web-application or Console application you can use Debug.WriteLine() to print to output console or you can say "output" -> "Debug" tab.

I tried using it in C# .NET Core web-application using below C# Code

      public IActionResult Index()
        {
            var orders = _context.Orders.ToList();
            Debug.WriteLine("First Order Country is : " + orders.FirstOrDefault().Country);

            return View(orders);
        }

and here is the output:

C#-console-log

You will need to add "using System.Diagnostics;" namespace.

Note: In Visual Studio Click on "View" and then in "Output" you will see the log when running your application.

OR

You can also use Trace.WriteLine() instead of Debug.Writeline()

Difference between Trace.WriteLine() and Debug.WriteLine() is that Debug.Writeline() statements will not be included in the Release compilation by default, whereas Trace.Writeline statements will be included.

1
At:- 11/30/2022 3:14:35 PM
Thanks for detailed answer, I will use Trace.WriteLine() since I may need to get some details on server also. 0
By : Vinnu - at :- 11/30/2022 3:16:36 PM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use