C# Windows 服务中的 Console.WriteLine()?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8792978/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 04:27:43  来源:igfitidea点击:

Console.WriteLine() inside a Windows Service?

c#windows-servicestopshelf

提问by Thomas

I am currently using TopShelf with a Console Application to create a Windows Service. When I run the code as a console application I use a few Console.WriteLine() to output results. Once the code does what it is supposed to do I install the console application as a Windows Service.

我目前正在使用 TopShelf 和控制台应用程序来创建 Windows 服务。当我将代码作为控制台应用程序运行时,我使用了几个 Console.WriteLine() 来输出结果。一旦代码完成了它应该做的事情,我就会将控制台应用程序安装为 Windows 服务。

Are there any downsides with leaving the Console.WriteLine() code even though a Windows Service can not write to the console? Are there any risks of having unstable code if I leave the Console.WriteLine() in there?

即使 Windows 服务无法写入控制台,离开 Console.WriteLine() 代码是否有任何缺点?如果我将 Console.WriteLine() 留在那里,是否有代码不稳定的风险?

采纳答案by dtech

The output will simply be discarded.

输出将被简单地丢弃

In a Windows Service there is no Console so Console.Write* output is discarded. There are a number of alternatives:

  1. The System.Diagnostics.Trace class has a similar interface to the Console class so you could migrate your code quite easily to this.
  2. It can then be configured to output to a file. You can use the System.Diagnostics.EventLog class to write to the Event Log which you can then monitor using Event Viewer.
  3. You can use the third-party open-source log4net library which is very flexible.

在 Windows 服务中没有控制台,因此 Console.Write* 输出被丢弃。有多种选择:

  1. System.Diagnostics.Trace 类具有与 Console 类相似的接口,因此您可以很容易地将代码迁移到该类。
  2. 然后可以将其配置为输出到文件。您可以使用 System.Diagnostics.EventLog 类写入事件日志,然后您可以使用事件查看器进行监视。
  3. 您可以使用非常灵活的第三方开源 log4net 库。

回答by doogle

No, the console class will safely write to the STDOUT, but you will just not see the output.

不,控制台类将安全地写入 STDOUT,但您将看不到输出。

回答by Bill Crim

If you use the System.Diagnostics.Trace functionality, you can redirect the output using the listeners and switches. If you compile with the TRACE symbol, then code will be included. If you don't add the TRACE, then it won't be compiled into the project.

如果使用 System.Diagnostics.Trace 功能,则可以使用侦听器和开关重定向输出。如果使用 TRACE 符号进行编译,则将包含代码。如果不添加TRACE,则不会编译到项目中。

If you run your services as console for debugging, the Trace will output to the console by default. I've taken to using Trace instead of Debug or Console writes since I can, from the config file, output the trace information to any combination of files, screen, database, etc.

如果您将服务作为控制台运行以进行调试,则默认情况下 Trace 将输出到控制台。我已经开始使用 Trace 而不是 Debug 或 Console writes,因为我可以从配置文件中将跟踪信息输出到文件、屏幕、数据库等的任意组合。

回答by Pierre

The output was always discarded until Windows Server 2008R2. Leave a console.writeline() in a service installed on that OS, and you'll get an error 1067 when starting/running the service, depending on the position of the writeline().

在 Windows Server 2008R2 之前,输出总是被丢弃。将 console.writeline() 留在安装在该操作系统上的服务中,根据 writeline() 的位置,在启动/运行该服务时您将收到错误 1067。