Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

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

like image 778
dotnetcoder Avatar asked Dec 07 '22 04:12

dotnetcoder


2 Answers

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, e.g. by using SysIntenal's DebugView.

like image 53
Dirk Vollmar Avatar answered Dec 10 '22 03:12

Dirk Vollmar


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.

like image 26
wefwfwefwe Avatar answered Dec 10 '22 03:12

wefwfwefwe