开发者

How to use console output in an ASP.NET environment?

开发者 https://www.devze.com 2022-12-13 10:53 出处:网络
I would like to print some traces during the requests processing. But when I make Console.WriteLine(\"something\") in this environment, nothing is shown.

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 or开发者_运维问答der to use console to print these traces?


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.


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.


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.


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)


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).


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消