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

时间:2022-07-18 20:43:23

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

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

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

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

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

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

6 个解决方案

#1


26  

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

使用Debug.Write()并通过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.

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

#2


9  

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.

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

#3


7  

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

我知道这已经很晚了,但您可以使用以下类从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);
    }
}

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

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

#4


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)

您可以使用Response.Write将输出写入页面以进行调试,或者在javascript中使用alert,甚至可以写入日志文件。有很多方法可以获得调试输出。你也可以使用.Net的log4net(类似于log4j)

#5


3  

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

鉴于它是一个ASP.NET应用程序,我会这样做:

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

然后为页面或应用程序启用跟踪,然后转到〜/ Trace.axd查看结果(它们也可以在页面输出的末尾,具体取决于您选择的配置选项)。

#6


0  

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.

Console.WriteLine()写入命令行 - 而不是写入网页。使用Response.Write()写入网页或在Visual Studio调试器中启动应用程序以查看命令行输出。

#1


26  

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

使用Debug.Write()并通过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.

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

#2


9  

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.

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

#3


7  

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

我知道这已经很晚了,但您可以使用以下类从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);
    }
}

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

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

#4


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)

您可以使用Response.Write将输出写入页面以进行调试,或者在javascript中使用alert,甚至可以写入日志文件。有很多方法可以获得调试输出。你也可以使用.Net的log4net(类似于log4j)

#5


3  

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

鉴于它是一个ASP.NET应用程序,我会这样做:

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

然后为页面或应用程序启用跟踪,然后转到〜/ Trace.axd查看结果(它们也可以在页面输出的末尾,具体取决于您选择的配置选项)。

#6


0  

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.

Console.WriteLine()写入命令行 - 而不是写入网页。使用Response.Write()写入网页或在Visual Studio调试器中启动应用程序以查看命令行输出。