Getting the category text from EventLogEntry.Category
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?
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
精彩评论