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
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:
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.
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly