简体   繁体   English

在C#中使用WMI读取事件查看器内容时,如何获取与文化无关的类型信息

[英]How do I get culture-neutral type information when reading event viewer content with WMI in C#

I am reading logs from EventViewer using below lines of code. 我正在使用下面的代码行从EventViewer中读取日志。

var searcher = new ManagementObjectSearcher(@"\\WS2012-DE01\root\cimv2",
               "SELECT * FROM Win32_NTLogEvent WHERE  Type ='Error'");

above code works fine in en-US culture, but will fail in other culture because other culture will be representing Error as some other word. 上面的代码在en-US文化中运作良好,但在其他文化中会失败,因为其他文化将代表Error作为其他一些词。

eg: Error word in de-DE culture(german) represents as Fehler . 例如: de-DE文化中的错误词(德语)表示为Fehler I will be using the same code in different environement. 我将在不同的环境中使用相同的代码。 I do not want to maintain a resource file since issue is with only one word or not need a Translator API because of security measures to solve this. 我不想维护资源文件,因为问题只有一个字或者不需要翻译API,因为安全措施可以解决这个问题。 Could anyone please provide me a solution. 有谁能请我提供解决方案。

Don't query filtering by the name of the event type, but filtering by the internal type id: 不要按事件类型的名称查询过滤,而是按内部类型id过滤:

var searcher = new ManagementObjectSearcher(@"\\WS2012-DE01\root\cimv2",
               "SELECT * FROM Win32_NTLogEvent WHERE EventType=1");

You can see the list of possible values for EventType in the documentation of the WMI Win32_NTLogEvent class . 您可以在WMI Win32_NTLogEvent类文档中看到EventType的可能值列表。

Note that the property Type is a string and contains the type in the local language, while EventType is an integer with a fixed meaning like 请注意,属性Type是一个字符串,包含本地语言的类型,而EventType是一个具有固定含义的整数

  • 1 = Error 1 =错误
  • 2 = Warning 2 =警告
  • 3 = Information 3 =信息
  • 4 = Security Audit Success 4 =安全审计成功
  • 5 = Security Audit Failure 5 =安全审核失败

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM