简体   繁体   English

在Windows中访问和查询事件日志

[英]Accessing and Querying Event Log in Windows

I was wondering how I could reach event Log entries. 我想知道如何才能达到事件日志条目。 I have a client server application and it executes without problems. 我有一个客户端服务器应用程序,它执行没有问题。 What i am looking for is all instances of log with the id of 1149. This log is of the remote connection entries. 我正在寻找的是id为1149的所有日志实例。这个日志是远程连接条目。 I have taken a piece of code, here it is. 我已经拿了一段代码,就在这里。

string logType = "System";
string str = "";
EventLog ev = new EventLog(logType, System.Environment.MachineName);
int LastLogToShow = ev.Entries.Count;
if (LastLogToShow <= 0)
    Console.WriteLine("No Event Logs in the Log :" + logType);

// Read the last 2 records in the specified log. 
int i;
for (i = ev.Entries.Count; i >= LastLogToShow - 1000 ; i--)
{
    EventLogEntry CurrentEntry = ev.Entries[i];
    if (CurrentEntry.InstanceId == 1149)
    {
        str += "Event type: " + CurrentEntry.EntryType.ToString() + "\n" +
               "Event Message: " + CurrentEntry.Message + CurrentEntry + "\n" +
               "Event Time: " + CurrentEntry.TimeGenerated.ToShortTimeString() + "\n" +
               "Event : " + CurrentEntry.UserName +"\n" +"\n";
    }
}
ev.Close();
return str;

The thing is I get the 42567 index is out of bounds exception everytime. 问题是我每次都得到42567索引超出界限。 I also dont know if it will work after that, so questions may follow. 我也不知道在那之后它是否会起作用,所以可能会出现问题。

EDIT: Indeed, the problem was me reaching out of the eventlog with my index like you guys said. 编辑:事实上,问题是我和我的索引一样伸出了事件日志,就像你们说的那样。 Using this line for the loop solved my problem here and I am able to reach the eventlog now, if anyone is looking around, this solution worked for me, so thank you all so much. 在循环中使用这一行解决了我的问题,我现在能够访问事件日志,如果有人在四处寻找,这个解决方案对我有用,所以非常感谢你们。

for (i = ev.Entries.Count - 1; i >= 0; i--)

This for (i = ev.Entries.Count; i >= LastLogToShow - 1000 ; i--) is causing your error. for (i = ev.Entries.Count; i >= LastLogToShow - 1000 ; i--)导致您的错误。 I don't really get what you're trying to do here. 我真的没有得到你想要做的事情。 For one if you have less than 1000 entries, your i can be negative. 例如,如果您的参赛作品少于1000个,那么您的i可能是负数。 When you use a negative value as the index of an array you will get "index is out of bounds exception". 当您使用负值作为数组的索引时,您将获得“索引超出范围的异常”。 When you are trying to process only the last 2 records (as your commentary above the for-loop suggests) you should just use this: 当您尝试仅处理最后2条记录时(正如for循环建议上方的评论),您应该只使用它:

for (i = ev.Entries.Count - 1; i >= ev.Entries.Count - 2; i--)

Of course you will still have to check if there is more than 2 entries because if there are 0 entries, the code will still go into the for-loop and try to access the array with negative indexes: 当然,你仍然需要检查是否有超过2个条目,因为如果有0个条目,代码仍将进入for循环并尝试访问具有负索引的数组:

if(ev.Entries.Count < 2)
  return str;
for (i = ev.Entries.Count - 1; i >= ev.Entries.Count - 2; i--)

Edit: Also just noticed even if there are more than 1000 records, when you go into the for-loop for the first time you will have ev.Entries[ev.Entries.Count] . 编辑:即使有超过1000条记录,也只是注意到,当你第一次进入for循环时,你会有ev.Entries[ev.Entries.Count] Since array-indexes are zero-based you have to substract 1 from the count to get the last element of an array. 由于数组索引从零开始,因此必须从计数中减去1才能获得数组的最后一个元素。

I highly suggest you use C# Linq for this. 我强烈建议您使用C#Linq。

Add this namespace 添加此命名空间

using System.Linq;

Linq is very similar to SQL in how it works with the data. Linq与SQL在数据处理方式上非常相似。 In your case: 在你的情况下:

List<string> stringLogs = 
    ev.Entries
        .Where(t => t.InstanceId == 1149)
        .Select(t => GenerateLogString(t))
        .ToList();

public string GenerateLogString(EventLogEntry CurrentEntry)
{
    return
        string.Format("Event type: {0}\nEvent Message: {1}\nEvent Time: {2}\nEvent: {3}\n",
            CurrentEntry.EntryType.ToString(),
            CurrentEntry.Message + CurrentEntry,
            CurrentEntry.TimeGenerated.ToShortTimeString(),
            CurrentEntry.UserName)
}

You can then convert the string logs into a single string, like you have there. 然后,您可以将字符串日志转换为单个字符串,就像您在那里一样。

string str = string.Join("/n", stringLogs);

If you want to select the top 2 logs (as your commentary suggests), add a .Take(2) to the query, like below. 如果要选择前2个日志(如您的评论所示),请在查询中添加.Take(2),如下所示。

List<string> stringLogs = 
    ev.Entries
        .Where(t => t.InstanceId == 1149)
        .Take(2)
        .Select(t => GenerateLogString(t))
        .ToList();

You just need to equal i to ev.Entries.Count -1. 你只需要等于ev.Entries.Count -1。 i = (ev.Entries.Count -1) i =(ev.Entries.Count -1)

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

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