[英]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
两个简单的选择
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. 或者,让控制台监视器以合理的间隔从日志文件中读取更新,而不是尝试直接进入服务进程。
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.