C# 如何在 ASP.NET 环境中使用控制台输出?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1848540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use console output in an ASP.NET environment?
提问by Victor Rodrigues
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?
缺少什么,我需要做什么才能使用控制台打印这些跟踪?
采纳答案by CesarGon
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 tracefeature, 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 页面。此页面将显示您的应用程序的跟踪消息。
回答by ctford
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 调试器中启动应用程序以查看命令行输出。
回答by dcp
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 中使用警报,甚至写入日志文件。有很多方法可以获得调试输出。您还可以使用 .Net 的 log4net(类似于 log4j)
回答by Gonzalo
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 applicationand then just go to ~/Trace.axdto see the results (they can also be at the end of the page output, depending on the configuration option that you choose).
然后为页面或应用程序启用跟踪,然后转到~/Trace.axd以查看结果(它们也可以位于页面输出的末尾,具体取决于您选择的配置选项)。
回答by Gonzalo
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 目录。
回答by MichaelTaylor3D
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 中一样。