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?
- 20,816
- 57
- 73
- 92
- 12,753
- 25
- 112
- 158
-
1if you are in debug mode output window should show it? No? – adt Feb 05 '13 at 17:56
-
1@adt No. `Console.WriteLine` will not display in the output window because it is invoked by the Browser in ASP. – StoriKnow Feb 05 '13 at 18:15
5 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:
Here's an example of what this will all look like:
For further readings, check out this SO post.
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.
- 1,553
- 2
- 17
- 29
-
-
1what doesnt work? I can try to help if I have more information. – MichaelTaylor3D Jan 29 '15 at 12:13
-
1
-
1True, you need to make sure that the System.Web namespace is defined. Ill add that to my answer. – MichaelTaylor3D Mar 21 '16 at 17:59
-
I kept getting a "Newline in Constant" error, until I changed the scriptTag declaration to `static string scriptTag = " – Joe Coyle Feb 28 '19 at 13:57
You can use Debug.Writeline("debug information"). It will be displayed in the Output window.
- 25,247
- 27
- 110
- 174
- 1,149
- 10
- 26
-
1@Burgi It was also written and posted *before* the accepted answer was. – TylerH May 23 '19 at 14:30
In addition to Sam's answer, you may find Response.Write useful. In some situations - for example, when you are supporting legacy inline .aspx pages - it's more convenient to debug by writing out suspect values to the browser:
String myString = GetAStringFromSomewhere();
/* What did that method actually return, anyway?
NB: Remove this once I know! */
Response.Write(myString);
This is less practical in ASP.Net MVC, however, as your controllers will be compiled. In this case, you might as well be writing out your debugging information to a log file, using something like log4net.
- 24,220
- 5
- 65
- 105
Thanks +MichaelTaylor3D for the solution, I have further enhanced it a little bit to support the function during ajax partial postback. Remember to add reference to System.Web.Extension to support ScriptManager.
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);
Page page = HttpContext.Current.Handler as Page;
if (ScriptManager.GetCurrent(page).IsInAsyncPostBack)
{
ScriptManager.RegisterClientScriptBlock(page, page.GetType(), "log", "console.log('" + message + "')", true);
}
else
{
HttpContext.Current.Response.Write(log);
}
}
public static void ConsoleError(string message)
{
string function = "console.error('{0}');";
string log = string.Format(GenerateCodeFromFunction(function), message);
Page page = HttpContext.Current.Handler as Page;
if (ScriptManager.GetCurrent(page).IsInAsyncPostBack)
{
ScriptManager.RegisterClientScriptBlock(page, page.GetType(), "error", "console.error('" + message + "')", true);
}
else
{
HttpContext.Current.Response.Write(log);
}
}
public static void Alert(string message)
{
string function = "alert('{0}');";
string log = string.Format(GenerateCodeFromFunction(function), message);
Page page = HttpContext.Current.Handler as Page;
if (ScriptManager.GetCurrent(page).IsInAsyncPostBack)
{
ScriptManager.RegisterClientScriptBlock(page, page.GetType(), "alert", "alert('" + message + "')", true);
}
else
{
HttpContext.Current.Response.Write(log);
}
}
static string GenerateCodeFromFunction(string function)
{
return string.Format(scriptTag, function);
}
}
- 2,207
- 1
- 26
- 43