简体   繁体   中英

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. I also do console.writeline call for every log message I write into the log file. But since its a windows service the console.write line is not visible.

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.

updated: The volume of log is very frequent ( 50 messages every minute) , I would prefer not want to crowd windows event log for this. Using a cosole window helps to look at logs and exit on convenience

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.

Instead, it is probably easiest to change the Console.WriteLine calls into Trace.WriteLine . Then you can attach to these trace messages, eg by using SysIntenal's DebugView .

I think you'll struggle to attach a console to an existing service. Two easy options

  1. write to the windows application log
  2. write to a text file, which you can then open with wintail or similar

Re your update - number 2 would be best then.

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. This link would help you...

Send data via UDP and make a simple program to show the data.
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.

Thanks for all the help!

Or open the file with http://www.log-expert.de/ and tail away. You can even make it colorful!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM