简体   繁体   English

如何将C#控制台应用程序附加到正在运行的Windows服务?

[英]How can you attach a C# console application to a running windows service?

I have a windows service that generates logs as it does some execution. 我有一个Windows服务,它生成日志,因为它执行一些。 I also do console.writeline call for every log message I write into the log file. 我也为我写入日志文件的每条日志消息执行console.writeline调用。 But since its a windows service the console.write line is not visible. 但由于它的Windows服务,console.write行不可见。

I want to write a C# console application program that can attach to my service (already running) and it could just show all console.writeline messages that the process (my windows service) is generating. 我想编写一个C#控制台应用程序,它可以附加到我的服务(已经运行),它可以显示进程(我的Windows服务)正在生成的所有console.writeline消息。

updated: The volume of log is very frequent ( 50 messages every minute) , I would prefer not want to crowd windows event log for this. 更新:日志量非常频繁(每分钟50条消息),我宁愿不想为此事件日志聚集。 Using a cosole window helps to look at logs and exit on convenience 使用cosole窗口有助于查看日志并在方便时退出

Displaying a window from a service is not a good idea as you would have to find out the "correct" session (Windows allows several users to be logged on) and also requires the service to have access to the user's desktop. 从服务中显示窗口并不是一个好主意,因为您必须找到“正确”的会话(Windows允许多个用户登录),并且还要求服务可以访问用户的桌面。

Instead, it is probably easiest to change the Console.WriteLine calls into Trace.WriteLine . 相反,最简单的方法是将Console.WriteLine调用更改为Trace.WriteLine Then you can attach to these trace messages, eg by using SysIntenal's DebugView . 然后,您可以附加到这些跟踪消息,例如使用SysIntenal的DebugView

I think you'll struggle to attach a console to an existing service. 我认为你很难将控制台连接到现有服务。 Two easy options 两个简单的选择

  1. write to the windows application log 写入Windows应用程序日志
  2. write to a text file, which you can then open with wintail or similar 写入文本文件,然后可以使用wintail或类似文件打开

Re your update - number 2 would be best then. 重新更新 - 数字2将是最好的。

You can make the service interactive, in which case you'll see the console window, but you'll always see it, and I think if you close it, you'll stop the service. 您可以使服务具有交互性,在这种情况下您将看到控制台窗口,但您将始终看到它,我认为如果您关闭它,您将停止服务。

Alternatively, make your console monitor read updates out of the log file at a reasonable interval instead of trying to get into the service process directly. 或者,让控制台监视器以合理的间隔从日志文件中读取更新,而不是尝试直接进入服务进程。

I would suggest you to log messages to Windows Event Viewer, if don't have burning desire to show them on console window. 我建议你将消息记录到Windows事件查看器,如果没有强烈的愿望在控制台窗口显示它们。 This link would help you... 这个链接可以帮助你......

Send data via UDP and make a simple program to show the data. 通过UDP发送数据并制作一个简单的程序来显示数据。
You warrant and asynchronous operation and can even send to another host. 您保证和异步操作,甚至可以发送到另一台主机。

I used cygwin to tail on the log file thats created by log4net this seems to do the trick for me. 我使用cygwin来尾随log4net创建的日志文件,这似乎对我有用。

Thanks for all the help! 感谢您的帮助!

Or open the file with http://www.log-expert.de/ and tail away. 或者使用http://www.log-expert.de/打开文件并拖尾。 You can even make it colorful! 你甚至可以把它变得丰富多彩!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运行 C# 控制台应用程序作为 Windows 服务 - Running a C# console application as a Windows service C#:Windows控制台应用程序到Windows服务 - C#: Windows Console Application to Windows Service 在C#中,您可以将Windows窗体应用程序作为服务启动吗? - In C#, can you make a windows form application start as a service? C#Windows服务和控制台应用程序合二为一 - C# Windows Service and Console Application in one 如何使用C#Windows Service获取正在运行的应用程序 - How to Get Running Application with C# Windows Service C# Windows 控制台应用程序如何判断它是否以交互方式运行 - How can a C# Windows Console application tell if it is run interactively Appveyor + .net C#控制台/ Windows服务应用程序 - Appveyor + .net C# console/windows service application 如何将Windows应用程序附加到我的C#应用​​程序? - How to attach windows application to my c# app? 无法从使用c#(Windows 2003服务器)编写的Windows服务访问DCOM对象,但可以在作为控制台应用程序托管时访问该对象 - Unable to access DCOM object from Windows Service written in c# (Windows 2003 server) but can access the object when hosted as a console application 在Windows 8中运行但在Windows 7中未运行的C#服务 - The C# service running in Windows 8 but is not running in Windows 7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM