Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show Console.WriteLine output in my browser console or output window?

I wrote System.Console.WriteLine("How can I see this debugging information in a browser"); in the model of my ASP.NET MVC4 project. How can I see this debugging string in the browser console, or at least in Visual Studio?. I can't find it in the output window of Visual Studio. Maybe I need to install some plugin from NuGet?

like image 685
Maxim Yefremov Avatar asked Feb 05 '13 17:02

Maxim Yefremov


People also ask

How do I show Console Output in Visual Studio?

Press F11 . Visual Studio calls the Console.

Where does Console WriteLine write to?

Console. Writeline() shows up in the debug output (Debug => Windows => Output).

How do I view Console Output in Visual Studio 2010?

In Visual Studio 2010, on the menu bar, click Debug -> Windows -> Output. Now, at the bottom of the screen docked next to your error list, there should be an output tab. Click it and double check it's showing output from the debug stream on the dropdown list.


2 Answers

Console.WriteLine(...) will not be displayed. If you absolutely need to see output in the debugger, you'll have to use

System.Diagnostics.Debug.WriteLine("This will be displayed in output window");

and view it in the Output window. You can open the output window by going to Debug -> Window -> Output:

enter image description here

Here's an example of what this will all look like:

enter image description here

For further readings, check out this SO post.

like image 157
StoriKnow Avatar answered Oct 06 '22 21:10

StoriKnow


You can write to your Javascript console from your C# Code using the following class

using System.Web;

public static class Javascript
{
    static string scriptTag = "<script type=\"\" language=\"\">{0}</script>";
    public static void ConsoleLog(string message)
    {       
        string function = "console.log('{0}');";
        string log = string.Format(GenerateCodeFromFunction(function), message);
        HttpContext.Current.Response.Write(log);
    }

    public static void Alert(string message)
    {
        string function = "alert('{0}');";
        string log = string.Format(GenerateCodeFromFunction(function), message);
        HttpContext.Current.Response.Write(log);
    }

    static string GenerateCodeFromFunction(string function)
    {
        return string.Format(scriptTag, function);
    }
}

Works just like its JS version, it actually converts your message to JS and injects it into the page.

like image 32
MichaelTaylor3D Avatar answered Oct 06 '22 23:10

MichaelTaylor3D