Windows 服务正在运行但事件日志不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/476432/
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 11:53:09  来源:igfitidea点击:

Windows Service running but event logs not working

windowsloggingwindows-services

提问by kjayakum

I have a windows service listening to messages from a queue but the messages are not read from the queue.I created an event log to check for logs during service startup and shutdown but the logs are not written. I do not want to debug the service since it is a painful process.Is there a way to solve this issue.The messages need to be read by the service and written to a database.

我有一个 Windows 服务正在侦听来自队列的消息,但没有从队列中读取消息。我创建了一个事件日志来检查服务启动和关闭期间的日志,但没有写入日志。我不想调试服务,因为这是一个痛苦的过程。有没有办法解决这个问题。消息需要由服务读取并写入数据库。

回答by Kev

This certainly sounds as if the account that your windows service is running under doesn't have enough rights to write to the event log in question.

这听起来好像您运行 Windows 服务的帐户没有足够的权限写入相关事件日志。

Setting event log permissions for non-admin accounts can be a bit of a black art because you need to configure custom security descriptors using SDDL etc. However there's a very handy MS knowledgebase article on how to do this programmatically:

为非管理员帐户设置事件日志权限可能有点像魔术,因为您需要使用 SDDL 等配置自定义安全描述符。 但是,有一篇非常方便的 MS 知识库文章介绍了如何以编程方式执行此操作:

How to setup event log security programmatically using the .Net Framework

如何使用 .Net Framework 以编程方式设置事件日志安全性

I use this all the time now and it's as simple as:

我现在一直在使用它,它很简单:

int mask = EventLogSecurity.CustomSD_ALL_ACCESS;

string logName = "Application";
string domain = "MyMachineOrDomainName";
string account = "UserAccount";

EventLogSecurity.AddUserToEventLogCustomSD(logName, domain, account, mask);

回答by DOK

In my experience, the most common problem in writing to event logs is that the user does not have sufficient permission to write to the event log. Can you check the identity that the Windows service is running under, and verify that it has permission to write to the event log that you have created?

根据我的经验,写入事件日志最常见的问题是用户没有足够的权限写入事件日志。您能否检查运行 Windows 服务的身份,并验证它是否有权写入您创建的事件日志?

And, can you check the event log to see if any errors are being recorded when attempts are made to write to it?

并且,您能否检查事件日志以查看在尝试写入时是否记录了任何错误?

回答by Igal Serban

If you want to use logging instead of debugging don't use the event log. Use plain old text logs.

如果您想使用日志记录而不是调试,请不要使用事件日志。使用纯旧文本日志。