ASP.NET 中的 Console.WriteLine 在哪里? [英] Where does Console.WriteLine go in ASP.NET?
问题描述
在 J2EE 应用程序中(例如在 WebSphere 中运行的应用程序),当我使用 System.out.println()
时,我的文本变为标准输出,由 WebSphere 管理员映射到文件控制台.
In a J2EE application (like one running in WebSphere), when I use System.out.println()
, my text goes to standard out, which is mapped to a file by the WebSphere admin console.
在 ASP.NET 应用程序中(例如在 IIS 中运行的应用程序),Console.WriteLine()
的输出到哪里去了?IIS 进程必须有 stdin、stdout 和 stderr;但是标准输出是映射到 Windows 版本的/dev/null 还是我在这里遗漏了一个关键概念?
In an ASP.NET application (like one running in IIS), where does the output of Console.WriteLine()
go? The IIS process must have a stdin, stdout and stderr; but is stdout mapped to the Windows version of /dev/null or am I missing a key concept here?
我不问是否应该在那里登录(我使用 log4net),但是输出到哪里去了?我最好的信息来自这个讨论,他们说Console.SetOut()
可以改变TextWriter
,但是还是没有回答Console的初始值是什么,或者如何在config/中设置的问题运行时代码之外.
I'm not asking if I should log there (I use log4net), but where does the output go? My best info came from this discussion where they say Console.SetOut()
can change the TextWriter
, but it still didn't answer the question on what the initial value of the Console is, or how to set it in config/outside of runtime code.
推荐答案
如果您查看 .NET Reflector,你会发现如果一个进程没有关联的控制台,Console.Out
和 Console.Error
由 Stream.Null
(包裹在 TextWriter
中)支持,它是 Stream
的虚拟实现,基本上忽略所有输入,并且没有输出.
If you look at the Console
class in .NET Reflector, you'll find that if a process doesn't have an associated console, Console.Out
and Console.Error
are backed by Stream.Null
(wrapped inside a TextWriter
), which is a dummy implementation of Stream
that basically ignores all input, and gives no output.
所以它在概念上等同于 /dev/null
,但实现更加精简:没有实际 I/O 发生在空设备上.
So it is conceptually equivalent to /dev/null
, but the implementation is more streamlined: there's no actual I/O taking place with the null device.
另外,除了调用SetOut
,没有办法设置默认值.
Also, apart from calling SetOut
, there is no way to configure the default.
更新 2020-11-02:由于这个答案在 2020 年仍在收集选票,可能应该注意的是,在 ASP.NET Core 下,通常附有控制台.您可以配置 ASP.NET Core IIS模块通过 stdoutLogEnabled
和 stdoutLogFile
设置将所有 stdout 和 stderr 输出重定向到日志文件:
Update 2020-11-02: As this answer is still gathering votes in 2020, it should probably be noted that under ASP.NET Core, there usually is a console attached. You can configure the ASP.NET Core IIS Module to redirect all stdout and stderr output to a log file via the stdoutLogEnabled
and stdoutLogFile
settings:
<system.webServer>
<aspNetCore processPath="dotnet"
arguments=".MyApp.dll"
hostingModel="inprocess"
stdoutLogEnabled="true"
stdoutLogFile=".logsstdout" />
<system.webServer>
这篇关于ASP.NET 中的 Console.WriteLine 在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!