|
Just to get it out of the way, this isn't a homework thread. I've been out of school for quite some time now. If this somehow breaks a TL rule though, I'm sorry and I'll happily accept this being removed. That said...
I'm having a great deal of difficult figuring out how to redirect standard output of a running windows service to a simple console app. This is being done in C#. The service is really simple, it just console.writelines some text every few seconds. I want my console app to simply echo the service's output.
I got standard output redirection working just fine when redirecting output from one console app to another console app. Doing that for a service is proving difficult.
I used this to redirect from one console app to another: http://www.c-sharpcorner.com/UploadFile/edwinlima/SystemDiagnosticProcess12052005035444AM/SystemDiagnosticProcess.aspx
Doesn't seem like I can apply that to a service though as far as I can tell.
|
Just out of curiosity, why would you want a service to have any visual aspects anyway? If you need visual aspects, a service is probably not the right solution to the problem...
|
i wrote a service before which communicated with an app on the desktop. One viable option on C++ i believe was to use SendMessage() - part of the Win32 API - to send messages from the service to the application. However the option I ended up using as SendMessage wasn't exactly passing parameters correctly, was i had the computer essentially send UDP packets to itself... definitely not your most ideal solution but it works. Since you're using C# i'm going to assume that the .NET framework has some networking solution so you can use that.
So in your service you would have a loop or function or however you implement it that sends a data packet to your computer's ip address (127.0.0.1) and a specific port and just have the console app listen on that port for any packets and open the packet, get the string, and print the string.
That's atleast the way I would probably do it. As i said before, this is most likely NOT the best way to do it as i'm sure .NET has other ways of accomplishing this but rather this is the way I've done in the past (granted using C++..) and I know works.
I hope i answered your question and good luck! :3
|
Surely you don't need to use networking... I'm sure you can do what you're trying to do using Win32 interop
|
Yes i know but i'm just saying it is an option although probably not the most viable one.
|
On October 29 2009 08:08 prOxi.swAMi wrote: Just out of curiosity, why would you want a service to have any visual aspects anyway? If you need visual aspects, a service is probably not the right solution to the problem... Well, we had a server program that could either run as a console app or as a service. Running as a console app we could see what the server is doing via console output but had the disadvantage of shutting down if someone accidentally logged off, remote desktop'ed in, etc. If we run it as a service we get around those accidental interruption problems but then we can't check to see what's going on.
So the solution I was tasked with making is a C# front end that can start the original program as a service and then read the console output from it. Thus the C# front end could go down due to someone logging off but that wouldn't affect the service and if we ever need to check up on the server we just bring the front end back online so we can see the console output.
I'm not sure if that is the optimal solution but that is what I was told to make.
|
Germany2896 Posts
Why don't you simple write the service so it logs it's output to a file? You might even be able to do that by piping stdout to the file.
|
On October 29 2009 18:07 MasterOfChaos wrote: Why don't you simple write the service so it logs it's output to a file? You might even be able to do that by piping stdout to the file. The service already does log it's output to a file. Another reason for the new front end is to make the console output more readable via color coding and filtering options. Pretty sure color coding can be done on a console app as well, but as far as I know filtering the output isn't so easily done in a console app.
|
Little change in plans. The service is apparently not using console.writeline(), rather it is writing to an eventlog. I'm not familiar with the use of event logs, time to start reading up on that. Not sure if this makes things easier or harder.
If anyone has any ideas on how to grab the event log output from a C++ service and output that to a C# frontend it would be much appreciated.
Edit: Yeah, this event log stuff seems like it will make life a lot easier. Sorry for the mixup guys, thanks for the help.
|
Use Microsoft's Log Parser, it can read text files but also the "Windows Event Viewer such as System, Application, and Security", it can even be used remotely. Disclaimer: I haven't used it, but that's what their website states.
|
|
|
|