Console.WriteLine()Windows服务里面呢? [英] Console.WriteLine() inside a Windows Service?

查看:556
本文介绍了Console.WriteLine()Windows服务里面呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用TopShelf一个控制台应用程序创建一个Windows服务。当我运行的代码作为控制台应用程序我用了几个Console.WriteLine()来输出结果。一旦代码做的事情是应该做我安装控制台应用程序作为Windows服务。



是否有与离开Console.WriteLine命令(任何负面影响)的代码,即使Windows服务无法写入到控制台?是否有其不稳定的代码,如果我离开Console.WriteLine命令()在那里的任何风险?


解决方案

The输出将被丢弃




在Windows服务是没有控制台,以Console.Write *输出
丢弃。还有一些备选方案:




  1. 的System.Diagnostics.Trace类有一个类似的界面到
    Console类,所以你可以很容易地移植代码到这一点。

  2. 然后它可以被配置为输出到文件。您可以使用
    类System.Diagnostics.EventLog写入到事件日志然后你就可以监视使用事件查看器。

  3. 您可以使用第三方
    开源log4net的库,它是非常灵活的。



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.

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?

解决方案

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.

这篇关于Console.WriteLine()Windows服务里面呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆