43

If I have IF statement like below:

If currentdate >= rating1  then
    status = 2
ELSEIF currentdate >= rating2  then
    daylight_savings_status = 0
ELSEIF currentdate >= rating3  then
    daylight_savings_status = 1
ELSE 
    daylight_savings_status = 1
End If

Is there something like in javascript

console.log('test');

that I can test on the which IF statement is truth of the statement?

This way I be able to test it on the firebug(firefox).

John Saunders
  • 160,644
  • 26
  • 247
  • 397
anatp_123
  • 1,165
  • 2
  • 10
  • 25
  • 1
    are you asking to log server side code (c#/vb) or front end (Javascript) code in "ASP.Net". – snowYetis Apr 09 '15 at 21:11
  • @snowYetis both would be nice to know – anatp_123 Apr 09 '15 at 21:16
  • possible duplicate of [Asp.net mvc Console.WriteLine to browser](http://stackoverflow.com/questions/14713782/asp-net-mvc-console-writeline-to-browser) – CodeCaster Apr 09 '15 at 21:16
  • Or try [ELMAH](http://stackoverflow.com/questions/7441062/how-to-use-elmah-to-manually-log-errors), or [a log4net appender that writes Javascript](http://weblogs.asp.net/stevewellens/log4net-log-to-a-javascript-console). – CodeCaster Apr 09 '15 at 21:19
  • @CodeCaster yes and no... This could be for ASP.Net Webforms although, the solution is the same. – snowYetis Apr 09 '15 at 21:19

6 Answers6

75

For Server Side

C# & VB.Net Server Side - This will show in the Visual Studio Output window.

System.Diagnostics.Debug.WriteLine(log data here)

Client Side JavaScript/Jquery - This will show in the browser devtools console window. Works on all popular browsers.

console.log(log data here) 
amd
  • 20,637
  • 6
  • 49
  • 67
snowYetis
  • 1,517
  • 16
  • 28
  • 1
    And if you want to show the log in the browser devtools, but from a controller .cs file? – m3.b Jun 23 '23 at 00:25
12

I would try to output it to the clients console ONLY If the Console.WriteLine() function doesnt work for you.

Page.Response.Write("<script>console.log('" + msg + "');</script>");

Ultimately though you should try to write debug statements in your own console (with System.Diagnostics.Debug.WriteLine(...)) and not the client's.

NB System.Diagnostics.Debug.WriteLine(...); gets it into the Immediate Window in Visual Studio 2008.

Go to menu Debug -> Windows -> Immediate:

Max Alexander Hanna
  • 3,388
  • 1
  • 25
  • 35
1

I know this is a really old topic but I've had success with the following:

@Html.Raw("<script>console.log('foo bar')</script>");
LManer311
  • 57
  • 1
  • 7
1

The equivalent for console.log in asp.net is

Console.WriteLine("Hello World!");
0

For outputting to your server console, use :

Console.WriteLine("test");
kris
  • 11,868
  • 9
  • 88
  • 110
0

This may be a moot point, but I wanted to make a note on this. As @snowYetis answer tells you the correct way to write to the system console for debugging puposes. My "trick" to making this a little more bearable during development is making a helper class somewhere in your project for you to import and use. One of the helper methods I ALWAYS implement in my projects is this one:

public void DebugLog(String msg)
{
    System.Diagnostics.Debug.WriteLine(msg);
}

This way you only need to call your Helper.DebugLog(e.Message); when you want to log to console without having to dot your way into madness :)

Hope this helps someone

IncrediblePony
  • 645
  • 9
  • 31