Windows服务运行状况/错误跟踪

时间:2014-03-17 11:38:20

标签: c# service monitor

我们的客户使用的软件包括客户端> gateway->引擎。引擎和网关实现为Windows服务,有时在同一台机器上实现。我们想要的是某种类型的监视器,它将测试服务是否正在运行,响应时间是什么,错误跟踪等等。因此,只要服务出现问题,我们就可以获得所有数据,为什么和发生了什么...所以没有更多的远程访问,事件日志。我们的软件中是否有现成的解决方案?

微软运营经理怎么样?可以在我们的案例中使用吗?我开始探索它,但没有找到任何有用的信息。

感谢您的任何信息!

1 个答案:

答案 0 :(得分:0)

有一个windows api可以帮助监控Windows服务运行状态。

std :: wstring widestr = std :: wstring(str.begin(),str.end());     const wchar_t * widecstr = widestr.c_str();

SC_HANDLE scm = ::OpenSCManager(0, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ENUMERATE_SERVICE);

SC_HANDLE service = ::OpenService(scm, L"Audiosrv", SERVICE_QUERY_STATUS);
if(service == NULL)
{
    std::cout << "NO target service.Fail to monitor Windows Service!" << std::endl;
}
else
{
    std::cout << "Start to monitor Windows Service!" << std::endl;
}

SERVICE_NOTIFY serviceNotify = { SERVICE_NOTIFY_STATUS_CHANGE,onServiceNotifyCallback};

while(1)
{
    const DWORD result1 = ::NotifyServiceStatusChange(service,
                                                SERVICE_NOTIFY_RUNNING,
                                                &serviceNotify);

    ::SleepEx(INFINITE, TRUE); // Wait for the notification

    const DWORD result2 = ::NotifyServiceStatusChange(service,
                                        SERVICE_NOTIFY_STOPPED,
                                        &serviceNotify);
    ::SleepEx(INFINITE, TRUE); // Wait for the notification
}

::CloseServiceHandle(service);
::CloseServiceHandle(scm);

}

以下是整个示例项目: https://github.com/gogocanfly/MonitorWindowsService

希望这会对你有所帮助。