从EventLogEntry.Category获取类别文本
由于某种原因,EventLogEntry.Category 返回数字而不是类别的文本。它返回与 CategoryNumber 属性相同的内容,我认为这是不应该发生的。我在这里做错了什么,还是有不同的方法来获取 EventLogEntry 的类别文本?
下面是一些重现该错误的代码:
Console.WriteLine(new EventLog("System").Entries[0].Category);
这应该写入“无”或“服务状态事件”(或适合您的任何相关类别)之类的内容,但它会写入“(0)”。对我在这里做错了什么有什么想法吗?
For some reason, EventLogEntry.Category returns a number instead of the category's text. It returns the same thing as the CategoryNumber property, which I'm assuming isn't supposed to happen. Am I doing something wrong here, or is there a different way to get the category text for an EventLogEntry?
Here is some code to reproduce the error:
Console.WriteLine(new EventLog("System").Entries[0].Category);
This should write something like "None" or "Service State Event" (or whatever the relevant category is for you), but instead it writes "(0)". Any thoughts on what I'm doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是他们 API 中的一个错误。我认为这个问题不会很快得到解决。这只发生在 Windows Vista 和更高版本的操作系统中。因此,这实际上在 XP、2000、2003 等中可以正常工作。
但是,您应该考虑另一种方法。似乎这是以编程方式访问事件日志的较低级别方法。该技术是 Windows Management Instrumentation (WMI),并且在 .NET 和 C# 中都有一个接口。
查看以下链接:
Windows Management Instrumentation (WMI) 教程
在 C# 和 .NET 中使用 WMI 远程监控远程日志
阅读 Widsows 事件日志
This is a bug in their API. I don't think it'll be resolved anytime soon. This only happens in Windows Vista and later OS's. So this actually works correctly in XP, 2000, 2003, etc.
However, there's another approach you should be looking at. Seems like this is the lower level approach to programmatically access the event log. The technology is Windows Management Instrumentation (WMI) and there's an interface to it in .NET and thus C#.
Check out these links:
Windows Management Instrumentation (WMI) Tutorial
Monitoring Remote Log Remotely using WMI in C# and .NET
Reading Widsows Event Log