28

I would like to print some traces during the requests processing.

But when I make Console.WriteLine("something") in this environment, nothing is shown.

What is missing, what do I need to do in order to use console to print these traces?

1
  • 2
    What makes you think there is a console? Commented Dec 4, 2009 at 18:04

6 Answers 6

35

Use Debug.Write() and and watch the results come out through the debugger output window in the IDE.

Alternatively, use the ASP.NET trace feature, which is quite powerful. Once you have enabled tracing, you can navigate to the trace.axd page in your web app's root directory. This page will show the trace messages for your app.

Sign up to request clarification or add additional context in comments.

3 Comments

Could I create some dedicated output to my app? I found Debug.Write as the best alternative here to do the tracing, but other traces are being done by asp.net there, I only would like to see my traces.
Yes. Navigate to the trace.axd page in your web app's root directory. If tracing is enabled for your app, that page will show the trace messages for your app only.
I can't get debug.print or debug.write to go to Output window for VB Web app.
10

In addition to the methods already mentioned you can simply write to a log file:

File.AppendAllText(@"c:\log.txt", @"Debug Message Here!" + Environment.NewLine);

Of course you could use Server.MapPath to write the file in your web directory.

Comments

10

I know this is late, but you can write to your Javascript console from your C# script using the following class

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);
    }
}

That way you can see your log messages in real time as you click through the site, just as you would in js.

1 Comment

So this is the answer I was actually looking for, but is it really the simplest way? Sounds a little convoluted to have to print out a script tag and call console.log just to print the the JS console. Simply writing to the log would just be public static void ConsoleLog(string message) { Response.Write("<script>console.log('{0}')</script>") }. But still seems like it should be built in...
4

You can use Response.Write to write output to your page for debugging, or use alert in javascript, or even write to a log file. There are many ways to get debugging output. There's also a log4net for .Net that you can use (similar to log4j)

Comments

3

Given that it's an ASP.NET application, I would do:

Page.Trace.Write ("Something here");

Then enable trace either for the page or the application and then just go to ~/Trace.axd to see the results (they can also be at the end of the page output, depending on the configuration option that you choose).

Comments

1

Where are you looking for the output?

Console.WriteLine() writes to the command line - not to the webpage. Either use Response.Write() to write onto the webpage or start up your application in the Visual Studio debugger to look at the command line output.

1 Comment

I'm looking at the command line / output, none of them shows anything.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.