C # capture windows service messages

1

I am developing a small application in C # to "manipulate" windows services (specifically apache).

I have already managed to start, stop and other operations.

What I now want is to obtain the (messages) results that the service returns.

Example:

  • I modify the config file of apache and I save it (any line) that obviously generates an error in configuration syntax.
  • I try to start the apache service and it obviously does not start and the error "message" is logged in the windows event viewer.

What I want

Capture in my application and show it in a textbox that message returned by apache.

this is my code.

public void ManipularServicios(string serviceName, int Operation)
        {
            ServiceController sc = new ServiceController();
            sc.ServiceName = serviceName;
            try
            {
                switch (Operation)
                {
                    case 1: sc.Stop(); ; break;
                    case 2: sc.Start(); break;
                }
            }
            catch (InvalidOperationException e)
            {
                 txtErrores.Text = e.Message;
            }
        }
  

The part of the Catch never runs even though the service does not start, which is supposed to be an error.

    
asked by Jorny 17.05.2017 в 16:52
source

1 answer

0

The action of starting a service is not related to whether it stops later. You can listen to messages with the class EventLog ( see EventLog.EventLogWritten and EventLog.Entries ).

Alternatively, you can do the algorithm that services.msc uses: wait a short period, then see if the service is still started (with ServiceController.Status ).

    
answered by 20.05.2017 в 18:45