如何从 Windows 服务捕获标准错误输出?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/830825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:25:00  来源:igfitidea点击:

How to capture standard error output from a Windows service?

c++windowsserviceldap

提问by veefu

I have an application that makes use of the Mozilla LDAP library. We're diagnosing a problem involving the LDAP library failing to make a connection to the server. I'm attempting to get additional information from the LDAP library by tossing a debug version of the lib in with the application and enabling debug using ldap_set_opt. Unfortunately, I think the debug library is sending debug strings to standard error.

我有一个使用 Mozilla LDAP 库的应用程序。我们正在诊断一个涉及 LDAP 库无法连接到服务器的问题。我试图通过将 lib 的调试版本与应用程序一起放入并使用 ldap_set_opt 启用调试来从 LDAP 库中获取其他信息。不幸的是,我认为调试库正在将调试字符串发送到标准错误。

While I'm working on recompiling the LDAP client library again, hopefully enabling the option that makes it call OutputDebugString instead of streaming to stderr, a nice solution would be to capture the stderr output to a file. The application, though, is running as a Windows service.

当我再次重新编译 LDAP 客户端库时,希望启用使其调用 OutputDebugString 而不是流式传输到 stderr 的选项,一个不错的解决方案是将 stderr 输出捕获到文件中。但是,该应用程序作为 Windows 服务运行。

Anyone know how I could redirect stderr to a file for an application running as a service?

任何人都知道如何将 stderr 重定向到作为服务运行的应用程序的文件?

edit

编辑

I'm hoping not to have to modify any more of the service source code than I already have. Options in the service configuration would be ideal.

我希望不必修改比我已有的更多的服务源代码。服务配置中的选项将是理想的。

采纳答案by Naaff

Can you try manually redirecting stderr?

您可以尝试手动重定向 stderr 吗?

    FILE* stderr_redirect = freopen( "C:/stderr.log", "w", stderr );

    // Code that writes to stderr

    fclose( stderr_redirect );

Edit:

编辑:

There is no way to redirect stdout or stderr for a service other than to handle those streams inside your service yourself. Some services provide an option to redirect these messages to a file. You could either temporarily redirect these streams or add an option to your service to make it configurable next time you have a problem.

除了自己在服务中处理这些流之外,没有办法为服务重定向 stdout 或 stderr。某些服务提供了将这些消息重定向到文件的选项。您可以临时重定向这些流,也可以向您的服务添加一个选项,使其在下次遇到问题时可配置。

回答by Michael

If you are modifying the code to the service, you could call SetStdHandlein your ServiceMain:

如果您要修改服务的代码,则可以在 ServiceMain 中调用SetStdHandle

SetStdHandle(STD_ERROR_HANDLE, logFileHandle);