简体   繁体   English

使用C#在Windows Server 2003上查询EventLog

[英]Query EventLog on Windows Server 2003 using C#

I'm now building a Windows Event Log viewer and we have quite a few Window Server 2003 boxes. 我现在正在构建Windows事件日志查看器,并且我们有很多Window Server 2003框。 I'm using EventLogReader class to do the querying, but it requires Vista+ so cannot be run on Windows Server 2003. Although EventLog class is available but it is very slow. 我正在使用EventLogReader类进行查询,但是它需要Vista +,因此无法在Windows Server 2003上运行。尽管EventLog类可用,但速度很慢。 Any other choices do I have? 我还有其他选择吗?

Update: I'm not querying all the event logs, instead I'm querying event logs in a date range, is there a way to make it faster given that we only need event logs fall into a range? 更新:我不是在查询所有事件日志,而是在某个日期范围内查询事件日志,考虑到我们只需要事件日志落入某个范围,有没有一种方法可以使其更快? Now using EventLog class is extremely slow even for local box, unbearable for remote one. 现在,即使对于本地机器,使用EventLog类也非常慢,而对于远程机器则无法忍受。

I just had a play with logparser . 我只是在玩logparser Log Parser Studio that I suggested earlier is a UI for these set of tasks. 我之前建议的Log Parser Studio是这些任务集的UI。

Here is a logparser query: 这是一个logparser查询:

logparser.exe -i:EVT “SELECT TimeGenerated,EventID,EventType,EventTypeName,EventCategory,EventCategoryName,SourceName,
Strings,ComputerName,SID,Message FROM \\servername\Application WHERE TimeGenerated > ’2012-07-12 00:00:00′ AND EventType IN (1;2) ORDER BY TimeGenerated DESC” -o:CSV -q:ON -stats:OFF >> c:\temp\Events.csv

Replace the '\\servername\\Application' with your server details. 将“ \\ servername \\ Application”替换为您的服务器详细信息。

The -i:EVT tells it to query event log. -i:EVT告诉它查询事件日志。

Here is a quick LogParser reference that I use. 这是我使用的LogParser快速参考

The EventLog class is slow. EventLog类很慢。 The speed of access depends on the size of the event log. 访问速度取决于事件日志的大小。 In most server scenarios, they are allowed to get quite large before archiving. 在大多数服务器方案中,允许它们在归档之前变大。 The native Windows Event Viewer also supports remote log viewing which allows you to demonstrate how slow remote log viewing is. 本机Windows Event Viewer还支持远程日志查看,这使您可以演示远程日志查看的速度如何。

I think it is likely that the Entries property of an EventLog is ordered by date. 我认为EventLog的Entries属性可能按日期排序。 That means you could implement binary search of the entries (which Linq does NOT do by default) to speed things up a ton. 这意味着您可以对条目实施二进制搜索(Linq默认情况下不执行此操作)以加快处理速度。 Here's an example of a binary search extension: Can LINQ use binary search when the collection is ordered? 这是二进制搜索扩展的示例: 订购集合时LINQ可以使用二进制搜索吗?

This is either a duplicate if or related to Which approach is better to read Windows Event log in C#? 这是否是重复项,或者与之相关? 哪种方法更适合阅读C#中的Windows事件日志? WMI or EventLog . WMI或EventLog

In addition to the information provided by the link above consider limiting the amount of information before the log file events roll over. 除了以上链接提供的信息外,还应考虑限制日志文件事件翻转之前的信息量。

You can check out a tool I wrote for logparser. 您可以签出我为logparser编写的工具 It has a SQL-Like create screen that will give you good query examples you can use. 它具有一个类似于SQL的创建屏幕,它将为您提供可以使用的良好查询示例。

If you include LogParser in your application be careful about how you deploy it: logparser binaries distribution , you can see I included it separately. 如果将LogParser包含在应用程序中,请注意如何部署它: logparser Binaries distribution ,您可以看到我单独包含了它。

#1 why I'm stuck with EventLog class since EventLogReader is not supported on Win Server 2003 #1为什么我被EventLog类困住了,因为Win Server 2003不支持EventLogReader

I suggest you just borrow the code from Visual Log Parser . 我建议您只是从Visual Log Parser借用代码。

#2 it needs to query over multiple servers at once #2它需要一次查询多个服务器

LogParser is perfect for querying multiple servers & multiple logs. LogParser非常适合查询多个服务器和多个日志。 It is very handy viewing all logs combined and sorting by Time or even Grouping by occurrence. 查看所有合并的日志,并按时间排序,甚至按事件分组,都非常方便。

#3 I'm not querying all the event logs, instead I'm querying event logs in a date range, is there a way to make it faster given that we only need event logs fall into a range? #3我不是查询所有事件日志,而是查询某个日期范围内的事件日志,考虑到我们只需要事件日志落入某个范围内,有没有一种方法可以使其更快?

Yes, LogParser full on fly's, faster than a speeding train! 是的,LogParser全程飞行,比超速行驶的火车快! Here is how you query Evt logs by DateRange: 以下是按DateRange查询Evt日志的方法:

SELECT Extract_FileName(EventLog) AS EventLog, RecordNumber, TimeGenerated, EventID, EventType, EventTypeName, EventCategoryName, SourceName, ComputerName, Message FROM
\\servername\Application
WHERE TimeWritten > '2011-01-25 12:01:00'
AND TimeWritten < '2012-01-25 12:01:00'

#4 I tried it out and that Log Parser Studio seems only work on log files, not on local system or remote server. #4我尝试了一下,发现Log Parser Studio似乎仅适用于日志文件,不适用于本地系统或远程服务器。

Log Parser Studio is just a GUI for logparser, it should work, try this raw query (without LP Studio using LogParser directly) to get all logs in your domain: Log Parser Studio只是logparser的GUI,它应该可以工作,请尝试以下原始查询(不使用LogParser直接使用LP Studio)来获取您域中的所有日志:

LogParser "SELECT SourceName,TimeGenerated,TimeWritten,Message INTO filename.csv FROM \\Server\Application where Message Like '%mydomain.com%'" -o:CSV

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

相关问题 如何使用C#查询带有大量EventId的Windows Eventlog - How to query Windows Eventlog with a large set of EventIds using c# 如何使用C#在Windows Server 2008中写入事件日志? - How to write to eventlog in Windows Server 2008 using C#? 使用EventLog类在Windows 2003 Server上记录错误 - Logging errors on Windows 2003 Server using the EventLog class 使用C#向Windows Server 2003/2008中的防火墙添加例外 - Adding exception to firewall in Windows Server 2003/2008 using C# Windows Server 2003上的C#SocketAsyncEventArgs问题 - C# SocketAsyncEventArgs Issue on Windows Server 2003 如何使用C#/ Vb.Net在Windows Server 2003及更高版本上“删除-保护”文件或文件夹? - How to **delete-protect** a file or folder on Windows Server 2003 and onwards using C#/Vb.Net? 使用C#从Windows Server 2003的CA获取证书 - Get certificate from windows server 2003's CA using C# 如何使用C#在Windows Server 2003 Active Directory中设置TerminalServiceProfile路径? - How to set TerminalServiceProfile Path in Windows server 2003 Active Directory using C#? 使用在 Windows Server 2008 上运行的 C# 在 Exchange 2003 中创建邮箱 - Create mailbox in Exchange 2003 using C# running on Windows Server 2008 在Windows 2003 Server上运行C#应用程序时出错 - Error while running C# application on windows 2003 server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM