如何在ASP.NET环境中使用控制台输出?

Vic*_*ues 24 c# asp.net console

我想在请求处理期间打印一些跟踪.

但是当我在这个环境中创建Console.WriteLine("something")时,没有显示任何内容.

缺少什么,为了使用控制台打印这些痕迹我需要做什么?

Ces*_*Gon 28

使用Debug.Write()和观察结果通过IDE中的调试器输出窗口显示.

或者,使用非常强大的ASP.NET跟踪功能.启用跟踪后,您可以导航到Web应用程序根目录中的trace.axd页面.此页面将显示应用程序的跟踪消息.


小智 9

除了已经提到的方法之外,您只需写入日志文件:

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

当然,您可以使用Server.MapPath在Web目录中编写该文件.


Mic*_*r3D 9

我知道这已经很晚了,但您可以使用以下类从C#脚本写入Javascript控制台

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

这样,您可以在点击网站时实时查看日志消息,就像在js中一样.